Kris Boyd's first-half penalty had Steve Clarke's men on course for victory but Bruce Anderson equalised with 17 minutes remaining.
Lewis Ferguson scored the winner from a free-kick just three minutes from the end as Derek McInnes' men secured all three points.