The Dons had dominated the entire match but they were made to pay for their misses when MacDonald rifled in his seventh goal of the season in the 87th minute to snatch the points.
Jermaine Easter was foiled by superb last-ditch sliding tackles from Leon Legge and Pim Balkestein as he twice raced clean through onto Luke Chadwick passes in the first half.
Jason Puncheon and Chadwick (twice) were both narrowly off-target from long range, before Easter somehow fired Aaron Wilbraham’s cut-back straight at Bees goalkeeper Wojciech Szczesny from seven yards on the stroke of half-time.
It was a similar story after the break as Wilbraham was denied by Szczesny’s legs when he was through one-on-one with the goalkeeper before he headed Dean Lewington’s cross against the inside of the post in the 65th minute.
MacDonald won it for Brentford when he latched onto Marcus Bean’s flick in the box and fired low past Willy Gueret three minutes from the end – their first shot on target.