Goals from Ryan Bowman and Wes Fletcher sealed victory for York at Northampton, who remained four points adrift at the bottom of League Two.
The Cobblers started well as Darren Carter shot wide, Antonio German headed wide and Ishmel Demontagnac had his low drive saved.
But York broke the deadlock when Bowman headed home Josh Carson's cross.
They doubled their lead from the spot after Kevin Amankwaah impeded Michael Coulson and Fletcher converted.
VIEWS FROM THE DRESSING ROOM
Northampton Town caretaker manager Andy King told BBC Radio Northampton:
"We lost a game we needed to win. There was nice, neat football in the first half, in a deadpan game.
"We tried to up the tempo in the second half and create chances and we didn't do that.
"I just felt there wasn't the edge and the urgency there. They didn't look the same bunch.
"Too many of them were off the pace."
York City manager Nigel Worthington told BBC Radio York:
"We managed the game very well. We kept playing decent football and we got the all-important second goal.
"It's something to build on. It's nothing to get excited about.
"There's a hell of a long way to go. We want to push on but to get there there's a lot of hard work to go in."
Northampton Town 01 Duke 02 Amankwaah (Harriott - 74' ) 23 McGinty 04 Carter 12 Tozer Booked 06 Collins 32 Blair 17 Deegan 25 German 29 Hope (Moyo - 71' ) 07 Demontagnac (Emerton - 62' ) Substitutes 13 Snedker 16 Hooper 18 Reid 19 Emerton 20 Moyo 24 Toney 26 Harriott York City 24 Ingham 02 Oyebanjo Booked 03 Davies 14 Montrose 05 McGurk Booked 15 Lowe 12 Carson 10 Penn 19 Bowman 09 Fletcher (Jarvis - 86' ) 07 Coulson Substitutes 11 Jarvis (Platt - 92' ) 16 Fyfield 17 McDonald 18 Platt 20 Allan 25 Dickinson 37 Reed
Ref: Graham Scott