Northampton ended a nine-match winless run in all competitions with a crucial 2-1 victory in their League One basement battle at Gillingham.
Danny Powell's low drive gave the Cobblers the lead against the run of play before Lee Martin equalised for the managerless Gills.
But Matt Grimes' deflected free-kick after 73 minutes gave Jimmy Floyd Hasselbaink's side all three points as they leapfrogged their opponents in the drop zone.
The hosts nearly took the lead through Martin's fierce drive after 16 minutes, while Elliott List's shot soared wide after he brilliantly wriggled away from a crowd.
Just before the break, Powell found himself in space in a central position before striking in off the post from 25 yards out.
Aaron Pierre nearly doubled their lead after latching onto John Joe O'Toole's header but the centre-back's volley was well saved.
After 62 minutes, the Gills equalised when Josh Parker's searching ball found Martin, who neatly finished into the corner of the net.
But the visitors regained the lead when Grimes' free-kick from the right hit both posts and dropped in to give them victory.