LEE McEvilly fired home a last gasp extra-time winner to send Accrington Stanley into the fourth round of the GLS Conference Cup.
The Reds now face a home game against Tamworth but more importantly the win halted a run of three games without a victory.
They had been lack-lustre in the last three matches but turned in a strong performance against struggling York and deserved the win.
Stanley were shocked when Andy Bishop fired home a Kevin Donovan cross on 10 minutes but Jonathan Smith got the home side back on level terms on 34 minutes.
Stanley had the better of the chances with one, an Ian Craney chip from 40 yards out, earning plenty of praise from boss John Coleman who felt it would have been "goal of the century" but it just chipped over the crossbar.
There were chances for both sides in the dying minutes with another Smith header saved by the keeper, Paul Robinson's effort was wide for York and unmarked McEvilly unbelievably headed over in the 93rd minute.
Extra time started with Craney having two good chances but it looked like the game was heading for penalties. That was until McEvilly charged onto Rory Prendergast's ball and hit it first time low into the net.
"We needed that and I think we deserved it," said Coleman. "Lee was man of the match in my book. He led the line superbly, he was an handful and he could have had a couple more. Now we have to take that performance into our league form."