After arriving at Bootham Crescent just 40 minutes before kick-off, the Stags made a sluggish start when Ryan Brobbel marked his home debut with a third-minute opener.
But the visitors soon regrouped courtesy of Sam Clucas' penalty - his third goal of the season - and substitute Ollie Palmer's fortunate effort condemning the Minstermen to their fourth successive defeat.
Both teams ended the game with 10 men after Richard Cresswell and Martin Riley were sent off for an off-the-ball incident in the box.
City made a superb start as on-loan Middlesbrough midfielder Brobbel nodded home in the early exchanges following Ryan Jarvis' curling shot which crashed back off the upright.
Paul Cox's side levelled in the 21st minute through Clucas, who powered home from the spot after Lanre Oyebanjo brought down James Jennings in the area.
Clucas, a constant thorn in the York defence, was denied twice by goalkeeper Michael Ingham, but had a hand in the winner 14 minutes from time as his long-range shot deflected off Palmer and into the bottom corner.
Tempers boiled over 11 minutes from time, Cresswell, a replacement for Wes Fletcher four minutes earlier, and Riley both dismissed as a result of their spat in the area.