Chris Porter found the net for the hosts, but the Blades slipped to a second defeat in four games under boss Nigel Clough.
The home side applied pressure with Porter heading off target and Simon Lappin having a shot touched wide by Gills keeper Stuart Nelson.
Neill Collins also had a header cleared off the goal-line by Elliott Hewitt shortly before Porter equalised with a header from Lappin's corner in the 19th minute.
Blades striker Shaun Miller tried a shot on the turn which was saved by Nelson.
But Gillingham regained the lead two minutes after the re-start when substitute McDonald followed up to score after John Mousinho's header rebounded off the crossbar.
United's best chance to equalise fell to Porter, who headed wide, while both he and Miller also fired off target.
- Chris Porter
- Simon Lappin