A Matthew Blinkhorn goal deep into stoppage time completed a dramatic comeback for York against Stockport.
The Hatters looked on course for victory after Danny Rowe's powerful drive had given them the lead.
However, Jon Challinor's cross found its way to Jamie Reed to head home the equaliser and set up a frantic finish.
And, five minutes into injury time, substitute Blinkhorn's header from Patrick McLaughlin's delivery gave the hosts an unlikely victory.
VIEWS FROM THE DRESSING ROOM
York City manager Gary Mills told BBC Radio York:
"It was a test of character. My players have been magnificent for me this season and for this football club.
"We're not going to give up. We've worked since 1 July last year to get where we are now and it hasn't come easy.
"I've got a group of players in there now that are not just going to lie down no matter what. They'll keep going to the end and they proved that today. I thought they were fantastic.
"It's that stage of the season where it doesn't just become a physical thing it's a mental job."