The hosts took the lead on 32 minutes through the experienced Kris Boyd, the striker tucking home form the penalty spot after Shay Logan had clumsily upended Chris Burke.
Boyd made few friends in the away end as he emphatically celebrated the strike in front of the Dons fans in response to jibes about his weight, but it was the traveling supporters who would have the last laugh.
Youngster Bruce Anderson made an immediate impact after coming off the bench, sticking out a foot to divert Niall McGinn's shot beyond Jamie McDonald in the home goal to level the scores, before Ferguson took centre stage late on.
With just three minutes to go,the midfielder's moment came, the 19-year-old finding the back of the net with a low free-kick to secure the points for the visitors.