The Cobblers raced into a 2-0 lead against the League Two high-flyers only to be pegged back before the break.
Top-scorer Adebayo Akinfenwa gave Northampton a second-minute lead with his 14th goal of the campaign, firing home from just inside the box after Emyr Huws set him up.
The hosts doubled their advantage in the 14th minute when Robins defender Billy Jones could only manage to put the ball past goalkeeper Scott Brown.
But the Robins shot-stopper kept his side in the game, foiling Lewis Hornby, Ishmel Demontagnac and Keith Lowe as Northampton looked for a third goal.
Cheltenham got back on level terms with two goals in three minutes.
Darren Carter reduced the arrears in the 25th minute after Chris Zebroski did well to pull the ball back for the midfielder to net from close range. And Zebroski equalised two minutes later when he headed home a cross from Jones.
Former Cobblers striker Shaun Harrad went close just after the restart but Brown thwarted Akinfenwa who also headed wide.
And it was Cheltenham who won it in the 80th minute when Duffy scored after being picked out by Carter's pass.