The deadlock was broken early on as York took the lead in just the seventh minute.
Michael Rankine appeared to flick John McGrath's free-kick home, although Bees defender David Stephens may have got the final touch.
Ten minutes after their opener York almost doubled the lead but Graham Stack saved well at his near post to deny Matty Blair after some great work from Curtis Obeng.
Parity was soon restored in the 26th minute as Barnet's Andy Yiadom prodded home from close range after John Oster's delivery had caused problems in the box following a big deflection off Rankine.
The Bees then almost took the lead minutes after the restart with Jon Nurse seeing his shot trickle wide of the post after a neat turn on the edge of the area.
However, the turnaround was complete on 73 minutes as substitute Jake Hyde collected Oster's pass before firing low past Michael Ingham for the winner.