Adam Nowland and Patrick Agyemang grabbed the goals for The Dons, sandwiching a Nyron Nosworthy header for the home side.
The Dons were desperate to narrow the gap between themselves and Bradford - and were hugely buoyed to take the lead 24 minutes in.
Nowland was the scorer, lashing home after Wade Small's shot had only been parried by Jason Brown in the Gillingham goal.
Gillingham piled on the pressure and got themselves level when Nyron Nosworthy headed home a John Hills cross four minutes before the break.
The home support must have thought that they were set to go on and claim the points, but it was not to be the case - with the visitors again getting themselves in front, and this time finding no response.
It was Agyemang that notched the winner, firing home across Brown after a fine pass from Nigel Reo-Coker.