Lavery struck in the 91st minute after Gary Roberts had levelled for the visitors midway through the second half.
Lewis Baker put the Dons ahead in the first half but Paul Cook’s side produced their smash and grab raid after Dons skipper Dean Lewington saw red.
Chesterfield enjoyed the opening exchanges, as Ollie Banks dragged his effort just wide before Roberts smacked the Dons’ upright with a powerful volley.
But the Dons stunned Cook’s side when Baker netted a long-range opener after 27 minutes.
The on-loan Chelsea midfielder, providing cover for the injured Dele Alli, looped a curling shot from the edge of the box over Spireites keeper Tommy Lee and into the top corner.
Roberts notched his sixth of the season to level after 66 minutes, slotting coolly past Dons keeper Ian McLoughlin before Lewington received a second yellow for a clear block on Gboly Ariyibi.
And Chesterfield nicked the winner at the death as Lavery stabbed in from close range.