It was the home side who took the lead at the midway point of the opening period, James McPake's through ball being blasted past Jamie Langfield by Robert SNODGRASS.
Aberdeen levelled in the 33rd minute when Kyle MACAULAY accepted a pass from Bobby Thomson, his effort just crossing the line before being scrambled clear.
The decider came eight minutes after the restart when Mackie latched on to a long ball before unleashing a powerful shot which was parried by Ludovic Roy. The keeper's clearance fell kindly for MACAULAY who knocked the ball home.
Five minutes later Mackie saw another good effort saved by Roy and shortly afterwards last season's top scorer made way for Bagshaw.
Double scorer Macaulay was booked for the visitors along with Gary Dempsey.