Two goals in the space of 60 seconds on 88 and 89 minutes gave visitors Huddersfield their sixth straight win, moving them up to fifth in the table.
Substitute David Mirfin, on for the last ten minutes, made the first and scored the second when the game appeared to be heading for a goalless draw.
Terriers boss Peter Jackson was without topscorer Jon Stead and fellow striker Andy Booth through injury, while York player-boss Chris Brass welcomed back defenders Chris Smith and Darren Edmondson from suspension.
The visitors started brightly and Danny Schofield twice went close from long range before the home side created the best chance of the opening half.
Darren Edmondson's cross from the right was volleyed on the turn by Richard Cooper ten yards out, but Phil Senior brought off a fine diving save.
From City's first corner Richard Hope's goalbound header was knocked round the post by defender Nathan Clarke with loud appeals for handball.
A typical fiery Yorkshire derby sparked into life on the half hour when Lee Fowler caught Cooper in an aerial challenge on the edge of the box. A melee quickly followed and Cooper needed lengthy treatment for a head injury.
York eventually started to get on top playing some fine passing football on a sticky pitch, but found Terriers defender Clarke in fine form at the back.
The Terriers started the second period in total dominance. Fowler and Jonathan Worthington had shots from the edge of the box blocked before Mark Ovendale made a brilliant save to tip Schofield's 20-yard screamer over.
York hit back strongly, but from Darren Dunning's corner Smith and Hope were both unable to force the loose ball home from close range.
Defender Smith then surged into the opposition box and crossed for Lee Bullock to turn and shoot, but Senior brought off another brilliant low diving save.
With ten minutes to go substitute Mirfin replaced the tiring Jon Newby and he was at the centre of the dramatic late action.
First he flicked over Brass and on the run from 25 yards had a shot which keeper Ovendale carelessly spilled for Schofield to score from close range.
Then, from the restart, City immediately lost possession and as the ball was knocked forward Mirfin broke free and coolly fired home the second from 18 yards.