Kenny Jackett's side dominated the first half at the Priestfield Stadium, but the hosts got stronger as the game progressed and came closest to breaking the deadlock when Danny Kedwell struck the post late on.
McDonald then snatched them maximum points with one of the last kicks of the game when a corner fell to him to fire home and inflict a second away defeat of the season on title-chasers Wolves.
© Getty Images
Gills stopper Stuart Nelson was on hand to deny the visitors on several occasions in the first period, saving from Kevin McDonald's volley into the ground and also keeping out an early poke from the stretching Michael Jacobs.
His best save, though, came in the 39th minute as he dived full length to his right to parry behind an effort from Jacobs, after the forward had created his own opening with a weaving run past three players.
Jacobs was joined in attack by Leigh Griffiths and Jake Cassidy after the interval as Wolves pushed for a winner, but they were largely limited to shots from distance while Gillingham had the best opportunities.
A Gills substitute, Bradley Dack, did well down the right flank before squaring to Cody McDonald, whose 12-yard shot was well blocked by Wolves league debutant Aaron McCarey, who was deputising in goal after Wales international Wayne Hennessey refused to play for the Molineux outfit.
Nelson was on hand to comfortably save long-range efforts from Jacobs and Cassidy in the space of three minutes before Gillingham raced up the other end and hit the woodwork, when Kedwell shoved off Danny Batth and drilled low against the post with the outside of his boot.
They had one more sight of goal, though, and Cody McDonald took it. Wolves failed to clear a corner which bounced off the leg of Batth and into the path of the Gills poacher, who swept home from eight yards to earn them a victory that moves them five points away from the bottom four.