George Williams scored his first goal of the season five minutes from time to claw MK Dons out of the League 1 drop zone for the first time since January 20.
Williams just about got enough on Ed Upson's through ball to poke it past keeper Tomas Holy to pick up Dons' first ever victory at Priestfield.
Chuks Aneke had earlier given Dons a first half lead, before the visitors were pegged back by Navid Nasseri's header four minutes before the break.
The result, Dons' first away win in the league since September 30 when they beat Bury, lifts them to 20th in the table with the rest of the division playing tomorrow (Good Friday).
Dan Micciche made just one change from the side which started on Saturday against Blackpool, with Robbie Muirhead returning on the left flank as Ethan Ebanks-Landell dropped back to the bench, alongside Scott Golbourne, who featured for the first time since New Year's Day after recovering from a hip operation.
Without a win in their six visits to Priestfield, Dons started poorly and found themselves on the back foot from the off. Scott Wagstaff had plenty of joy down the Gillingham right as he capitalised on the space between Muirhead and Dean Lewington. His balls into the box caused havoc in the wind as Lee Nicholls struggled to get a read as the ball swirled in the air.
Without much bite going forwards, Dons barely threatened Tomas Holy's goal, while at the other end, Jake Hessenthaler somehow poked wide when Dons failed to deal with another Wagstaff ball into the mixer.
Having barely threatened in the opening half hour, Dons took the lead after 33 minutes when Ouss Cisse and Kieran Agard combined to put Chuks Aneke in behind, and drawing out Holy, Aneke coolly rolled home his 10th goal of the season.
It was perhaps cruel on Gillingham to be trailing, but they weren't for long. Luke O'Neill was given time on the right to cross deep to the far post, and it was expertly read by Navid Nasseri, who was making his full debut for the club, to head home from close range four minutes before the interval.
The second half showed little signs of improving as a spectacle, and only an Aneke effort from the edge of the box four minutes after the restart was worthy of note.
But while Gillingham looked the better in the first half, Dons were doing the work in the second period. After his 67 minute spell for England U20s on Tuesday night, he replaced the anonymous Peter Pawlett and began to pull the strings for the visitors. He should have won a penalty too with 25 minutes remaining when he was sent sprawling in the box, only to have the decision go against him, earning him a booking for diving.
The strange refereeing decisions continued too, when with 10 minutes to go Max Ehmer hauled back Aneke when he was through on goal, having latched on to Brandon Thomas-Asante's delicate through ball. Incredibly though, Ehmer was only booked.
Five minutes later though Dons would get their reward. Ed Upson burst through the midfield with the ball, and with two options available, he rolled in George Williams and he managed to get something on it to deceive Holy and give Dons the lead once again.
It was a nervy final few minutes for the visitors, though only a Connor Ogilvie effort troubled Lee Nicholls.
Referee: John Busby
Attendance: 5,540 (443)
Gillingham: Holy, O'Neill, Garmston, Ehmer, Zakuani, Wagstaff (Eaves 69), Hessenthaler, Wilkinson, Olgilvic, Byrne, Nasseri (Nugent 90)
Subs not used: Hadler, Lacey, List, Nash, Moussa
MK Dons: Nicholls, Lewington, Wootton, Ward, Williams, Muirhead (Ebanks-Landell 73), Upson, Cisse, Pawlett (Brittain 52), Aneke, Agard (Thomas-Asante 79)
Subs not used: Sietsma, Reo-Coker, Golbourne, Ugbo,
Booked: Pawlett, Brittain, Ehmer, Lewington, O'Neill