Defender Jon Maloney was York's hero as they stunned leaders Barnet.
Guiliano Grazioli's snap-shot cannoned back off the woodwork before Andy Bishop's cut-back was blasted home by Kevin Donovan to put York ahead.
Prolific striker Grazioli was on target 12 minutes after the break, pouncing to equalise from Dwaine Lee's pass.
But the home side claimed all three points when the on-loan Maloney's shot was parried away and he slid home the ball from the resulting corner.
York: Porter, Law, Groves, Maloney, Smith, Donovan, Webster,
Dunning, Merris, Bishop, Paul D Robinson.
Subs Not Used: Yalcin, Stockdale, Davies, Paul Robinson, Grant.
Booked: Merris, Webster, Donovan.
Goals: Donovan 45, Maloney 68.
Barnet: Gore, Hendon, Yakubu, King, Clist, Graham, Lee, Bailey,
Strevens, Grazioli, Roache (Hatch 70).
Subs Not Used: Charles, Lopez, Millard, McBean.
Booked: Hatch, Grazioli.
Goals: Grazioli 58.
Att: 1,926
Ref: D Foster (Tyne & Wear).