Dean Lewington scored a stoppage time winner as MK Dons threatened to throw away two points against Gillingham, winning 3-2 at Stadium MK.
What should have been an easy course to victory, leading 2-0 after 10 minutes thanks to goals from Robbie Muirhead and Harvey Barnes, was made much more difficult as the home side struggled to put them to bed.
Even when Cody McDonald pulled one back for the visitors 11 minutes after the restart, Dons had chances to make sure, with Kieran Agard, Ben Reeves and Dean Bowditch all squandering.
George Williams though would play his side into trouble when he downed McDonald in the final minute. Stand-on keeper Lee Nicholls saved Josh Wright's spot kick, but the Gills man was on point to convert the rebound and rescue a point.
But it wasn't to be as Lewington's header looped up and over keeper Tomas Holy to win it deep into injury time.
With safety the name of the game, Dons were dealt a blow with injury to goalkeeper David Martin, meaning Lee Nicholls would step into the breach to replace him in the only change from last Saturday's 0-0 draw with Port Vale.
With both sides flirting dangerously with the relegation zone, it was shaping up to be a must-win for both sides, but it was the home team which struck first in some style. Robbie Muirhead, who had drawn critics since signing in January, unleashed a rocket from 20 yards to beat Tomas Holy, who simply watched it sail into the top corner.
Dons drew more blood just five minutes later when George Williams drilled the ball to Kieran Agard, who in turn flicked in Harvey Barnes, who kept his cool to beat Holy for a second time, picking out the bottom corner to make it 2-0, leaving the visitors somewhat shell-shocked.
With Dons in a comfortable position though, they let off the gas a little and allowed Gillingham a spell on the ball. Bradley Dack was proving to be the leading man for the visitors, twice having decent efforts on goal - the first a corner almost catching stand-in keeper Nicholls unawares as it whipped goalwards, before seeing a deflected strike just wide of the upright.
With eight minutes of the first half to go though, Dons had two glorious chances to put the game to bed, the visitors were kept in it by keeper Holy. First, George Baldock squirmed into space but fired straight at the stopper, but the rebound looked almost certain to be convered by Dons' top scorer Agard, only again to be denied by the Czech.
It looked as though the second half would continue in a similar vain as the first as Dons poured forwards in the early exchanges, with half chances for Agard and Stuart O'Keefe.
But the game was turned on it's head when Cody McDonald's audacious flick on Lee Martin's cross caught Nicholls flatfooted as it was turned into the net, halving the lead 11 minutes after the restart.
Given a new lease of opportunity now, Gillingham looked a different entity in the second half and but for a big save from Nicholls, could have been level when McDonald again broke in behind the back line but fired at the legs of the keeper.
Dons though weren't without their own chances to restore a two goal lead, but once again drew a brilliant save from Holy to deny Agard from close range as they sought to retake control of the game.
It would have been much more comfortable for the home side had Ben Reeves converted his free kick, once again denied by Holy, while substitute Dean Bowditch missed an open goal from the rebound.
In a remarkable turnaround though, Gillingham felt they had snatched a point in the last minute when Williams downed McDonald in the penalty area, earning him his second yellow card and marching orders. While Nicholls did the business to keep out Josh Wright's penalty, the rebound fell kindly to him and he put home what everyone felt would be the equaliser.
But it wasn't to be. Reeves' free kick from just inside the Gillingham half was hopefully pitched in to the mix, and with Holy coming into No Man's Land to try and claim it, Dean Lewington watched his header loop up and over the line to snatch it at the death. Bradley Dack was sent off for his protests in the aftermath.
The win should spell safety for Dons now too, eight points clear of the relegation zone in 14th with six games remaining.
Referee: Carl Boyeson
MK Dons: Nicholls, Williams, Walsh, Lewington, Baldock, Upson (Potter 69), O'Keefe, Reeves (Downing 90), Barnes, Muirhead, Agard (Bowditch 69)
Subs not used: Burns, Ngombo, Brittain, Maynard
Gillingham: Holy, Jackman (List 78), Ehmer, Oshilaja, McDonald, Martin, Dack, Rehman, Byrne, Cornick (Parker 77), Wright
Subs not used: Nelson, Hessenthaler, Donnelly, Quigley, Muldoon
Booked: Ehmer, Dack, Williams, Baldock, Oshilaja, Bowditch
Sent off: Williams, Dack