Harry Pell scored a late winner to haul Wimbledon out of the relegation zone and condemn Nigel Worthington to a losing start as York City boss.
Mat Mitchel-King's half-volley put the Dons in front, but Chris Smith levelled before the break with a header.
Brennan Dickenson took the ball past keeper Michael Ingham to restore the home side's lead, only for Smith to bundle in another equaliser.
But Pell finished off a post to lift Wimbledon from bottom to 21st.
The former Hereford United midfielder's 79th-minute strike, the January deadline signing's second goal in three games, lifts the Dons to within a point of the Minstermen in 19th place.
VIEW FROM THE DRESSING ROOM
AFC Wimbledon boss Neil Ardley told BBC London 94.9:
"I said to the players 'Are you trying to make me go grey?'
"It was a dramatic win but I asked them to show character earlier this season and they showed bundles of it today.
"I felt the performance levels of the boys were excellent.
"We were the better team and that doesn't always work, but I was proud of them."
City manager Nigel Worthington told BBC Radio York:
"I thought the players' participation in that was outstanding. They kept going right until the final whistle, which showed a lot of character.
"But, if you score two goals away from home, you should be getting something from the game.
"There was a lot to deal with but we dealt with it and things we've been working on, a lot of them came off but there's one or two we've got to continue to work on.
"But, for their commitment and their energy, I can't fault the players at all."