Gillingham needed a stoppage-time penalty from Danny Kedwell to see off the challenge of Northampton.
Jo Kuffour put the Gills in front with a neat finish and doubled the lead with a backheel before Saido Berahino made it 2-1 with a 25-yard strike.
Kuffour completed his hat-trick from the spot after he was felled before Kelvin Langmead's superb angled shot and Berahino's lash home made it 3-3.
But Kedwell gave the Gills the points after Frank Nouble was fouled.
The result leaves Northampton just three points clear of the relegation zone while Gillingham are now two points outside the play-off places.
The Cobblers have failed to register a win since 1 October.
VIEWS FROM THE DRESSING ROOM
On-loan Gillingham striker Jo Kuffour told BBC Radio Kent:
"It was nice. Every striker dreams of that. I'm just glad it was me today.
"I thought we did well but we could have done better and it's nice to get the three points at the end.
"I love it here, there's a great bunch of lads. Hopefully we can agree something.
"It's out of my hands but I would love to stay but it's up to Gillingham and Bristol Rovers."
Northampton Town manager Gary Johnson told BBC Radio Northampton:
"You go through every emotion that the supporters go through. There was some great attacking and some really poor defending.
"We took the game to them in the second half and had three or four unbelievable chances to have won the game.
"You've got to look at the back four's defending. We look like we're going to score now, we've got a shape that looks good going forward but we have to start making teams work for their goals."
Live Text Commentary
Full Time The final whistle goes and the game is over.90:00 +3:54
John Johnson takes a shot. Blocked by Matt Lawrence.90:00 +0:53 Goal scored
Goal! - Danny Kedwell - Gillingham 4 - 3 Northamp'n Penalty taken right-footed by Danny Kedwell and scored. Gillingham 4-3 Northampton.89:55 Booking
Booking Kelvin Langmead booked for unsporting behaviour.89:43
Unfair challenge on Frank Nouble by Kelvin Langmead results in a Penalty.86:53
Andy Frampton concedes a free kick for a foul on Adebayo Akinfenwa. Michael Jacobs produces a shot on goal direct from the free kick, Sam Walker makes a clearance.85:23
Michael Jacobs shoots direct from the free kick.85:23 Substitution
Substitution Derek Niven leaves the field to be replaced by Paul Turnbull.85:23
Free kick awarded for an unfair challenge on Saido Berahino by Curtis Weston.82:48 Substitution
Substitution Chris Whelpdale goes off and Curtis Weston comes on.82:09
Luke Rooney takes a shot. Sam Walker makes a save.79:56
Inswinging corner taken by Danny Jackman, save by Sam Walker.79:28
Jake Robinson takes a shot. Save made by Ross Flitney.78:20 Substitution
Substitution Chris Arthur goes off and Jake Robinson comes on.72:52
Derek Niven gives away a free kick for an unfair challenge on Luke Rooney. Direct free kick taken by Danny Jackman.71:39 Substitution
Substitution Luke Rooney replaces Charlie Lee.69:17
Foul by Kelvin Langmead on Danny Kedwell, free kick awarded. Direct free kick taken by Danny Jackman.68:42
Shot from deep inside the area by Adebayo Akinfenwa clears the bar.67:43
Lewis Young takes a shot. Save by Ross Flitney.66:09
Handball by Adebayo Akinfenwa. Ross Flitney takes the direct free kick.
Shot from just outside the area by Danny Jackman goes over the bar.64:26
Saido Berahino concedes a free kick for a foul on Matt Lawrence. Matt Fish restarts play with the free kick.63:34
Lewis Young takes a shot. Ross Flitney makes a save.63:03
The assistant referee signals for offside against Saido Berahino. Free kick taken by Ross Flitney.61:16
Jo Kuffour is ruled offside. Free kick taken by Sam Walker.59:09
Assist on the goal came from Michael Jacobs.59:09 Goal scored
Goal! - Saido Berahino - Gillingham 3 - 3 Northamp'n Saido Berahino gets on the score sheet with a goal from the edge of the area to the top right corner of the goal. Gillingham 3-3 Northampton.58:30
Joe Martin concedes a free kick for a foul on Lewis Young. The free kick is delivered right-footed by Michael Jacobs from right wing,56:21
Effort on goal by Saido Berahino from just inside the penalty area goes over the target.55:50
Chris Whelpdale takes a shot. Sam Walker makes a save.53:10
Assist by Sam Walker.53:10 Goal scored
Goal! - Kelvin Langmead - Gillingham 3 - 2 Northamp'n Kelvin Langmead fires in a goal from inside the box to the bottom right corner of the goal. Gillingham 3-2 Northampton.51:20
Danny Jackman challenges Derek Niven unfairly and gives away a free kick. Direct free kick taken by Sam Walker.49:42
The assistant referee flags for offside against Danny Kedwell. Sam Walker takes the free kick.47:53
Saido Berahino concedes a free kick for a foul on Matt Lawrence. Andy Frampton takes the direct free kick.47:26
Shot by Danny Kedwell from outside the box goes over the target.47:05
Danny Jackman decides to take a short corner.46:42
Jo Kuffour takes a shot. Blocked by John Johnson.45:01
The referee gets the second half started.45:00 +4:26 Half time
Half Time The ref blows to end the first period.45:00 +2:36
Corner taken left-footed by Danny Jackman from the left by-line, Kelvin Langmead makes a clearance.45:00 +1:51
Michael Jacobs takes a shot. Ross Flitney makes a save.45:00 +0:34 Goal scored
Goal! - Jo Kuffour - Gillingham 3 - 1 Northamp'n Jo Kuffour scores a placed penalty. Gillingham 3-1 Northampton.44:51 Booking
Booking Chris Arthur goes into the book for dissent.44:39 Booking
Booking Adebayo Akinfenwa receives a yellow card for dissent.44:03
Penalty awarded for an unfair challenge on Jo Kuffour by John Johnson.42:40
Charlie Lee takes a shot. Derek Niven makes a clearance.41:07
Foul by Adebayo Akinfenwa on Andy Frampton, free kick awarded. Ross Flitney restarts play with the free kick.38:30
Inswinging corner taken by Michael Jacobs.37:49 Substitution
Substitution Matt Fish comes on in place of Garry Richards.35:44
Derek Niven takes a shot. Save made by Ross Flitney.34:25
Adebayo Akinfenwa provided the assist for the goal.34:25 Goal scored
Goal! - Saido Berahino - Gillingham 2 - 1 Northamp'n Saido Berahino grabs a goal from inside the box to the bottom right corner of the goal. Gillingham 2-1 Northampton.32:31
Inswinging corner taken left-footed by Danny Jackman from the right by-line, Kelvin Langmead manages to make a clearance.31:27
Lewis Young is flagged offside by the assistant referee. Free kick taken by Ross Flitney.29:57
Chris Whelpdale provided the assist for the goal.29:57 Goal scored
Goal! - Jo Kuffour - Gillingham 2 - 0 Northamp'n Jo Kuffour fires in a goal from inside the six-yard box to the bottom left corner of the goal. Gillingham 2-0 Northampton.27:06
Danny Jackman has shot on goal from just outside the area which goes wide of the right-hand upright.25:11
Corner taken left-footed by Danny Jackman from the right by-line, Headed effort from deep inside the penalty area by Danny Kedwell misses to the right of the target.24:03
Michael Jacobs takes a shot. Save by Ross Flitney.22:58
Effort from just outside the box by Danny Kedwell goes over the bar.22:04
Free kick taken by Ashley Westwood.22:04 Booking
Booking Charlie Lee goes into the referee's book for unsporting behaviour.21:41
Michael Jacobs fouled by Charlie Lee, the ref awards a free kick.19:46
Free kick awarded for a foul by Ashley Westwood on Garry Richards. Free kick taken by Ross Flitney.19:19
Michael Jacobs takes a short corner.18:06
Free kick awarded for an unfair challenge on Adebayo Akinfenwa by Joe Martin. Michael Jacobs takes the direct free kick.15:09
Garry Richards gives away a free kick for an unfair challenge on Michael Jacobs. Ashley Westwood takes the free kick.13:39
Free kick crossed by Danny Jackman, Ashley Westwood manages to make a clearance.13:39 Booking
Booking Booking for Michael Jacobs for unsporting behaviour.13:22
Foul by Michael Jacobs on Charlie Lee, free kick awarded.11:47
Joe Martin gives away a free kick for an unfair challenge on Lewis Young. Free kick taken by Ashley Westwood.10:05
The assist for the goal came from Matt Lawrence.10:05 Goal scored
Goal! - Jo Kuffour - Gillingham 1 - 0 Northamp'n Jo Kuffour scores a goal from close range low into the middle of the goal. Gillingham 1-0 Northampton.9:43
Effort from close in by Saido Berahino bounces off the post.9:21
Michael Jacobs takes a shot. Save made by Ross Flitney.8:05
Shot by Adebayo Akinfenwa. Ross Flitney makes a save.7:21
Ben Tozer restarts play with the free kick.7:21 Booking
Booking The referee shows Andy Frampton a yellow card for unsporting behaviour.7:06
Saido Berahino fouled by Andy Frampton, the ref awards a free kick.5:24
Foul by Chris Arthur on Danny Kedwell, free kick awarded. Danny Jackman restarts play with the free kick.4:23
The assistant referee flags for offside against Frank Nouble. Free kick taken by Sam Walker.2:32
Danny Kedwell is ruled offside. Sam Walker takes the indirect free kick.1:26
Adebayo Akinfenwa takes a shot. Clearance by Andy Frampton.0:00
The referee gets the game started.
Live data and text provided by our data suppliers