In a drab opening quarter of the match neither side created any meaningful chances, with both sides cancelling out one another in a congested midfield.
The closest either side came when visitors' striker John Sutton skipped inside his marker but his low shot from 25 yards was easily held by Jamie Langfield.
Aberdeen finally got their act together on 27 minutes, but Barry Nicholson drove over the crossbar after being put in by Russell Anderson.
Clever play by Gary Dempsey on 35 minutes saw him deceive two defenders before clipping a great ball into the path of Steve Lovell, whose first-time effort was too close to keeper Tony Bullock.
On 63 minutes Jamie Smith's corner found the head of Scott Severin whose effort caused panic in the Buddies rearguard, before Andy Millen desperately hooked the ball over his own bar.
Aberdeen finally made the breakthrough after 78 minutes. Dempsey's free-kick found Lee Miller, who headed into the path of Mackie and he gleefully stabbed the ball home from eight yards.
The Dons secured all three points when Michael Hart, Miller and Smith combined to play in Mackie for his second goal.