Simeon Jackson's late double earned Gillingham a vital win and all but condemned Luton Town to relegation.
Jackson bravely headed home from close range in the 75th minute after the home side had endured a frustrating evening in front of goal.
And the striker turned himself into a hero with the home fans by heading home Nicky Southall's cross to claim all three points for Gillingham with two minutes remaining.
Sam Parkin had given the visitors a 19th minute lead when he looped a header over stranded keeper Derek Stillie from 15 yards.
Parkin missed arguably an easier chance from an Ed Asafu-Adjaye cross five minutes later after inexplicably heading wide with a goal at his mercy.
A spell of home pressure followed and Luton keeper Dean Brill was called upon to push away a fierce Dennis Oli drive from Adam Miller's incisive pass.
But the Hatters remained a threat on the break and Matthew Spring's drive on 38 minutes almost doubled their lead but whistled just wide of Stillie's right-hand post.
The visitors threw men behind the ball after the break as Gillingham poured forward and Brill was at his best to deny Jackson on 62 minutes after the striker had found himself one-on-one, before Sol Davis produced a heroic goalline clearance.
Parkin missed another chance to put clear daylight between his side and the Gills when he curled wide from the edge of the box after neatly side-stepping two defenders.
But as the home supporters began to fear defeat, Jackson headed home a loose ball after Brill had parried substitute Leroy Griffiths' shot into his path.
With a point less than sufficient for Gillingham, the home side continued to lay siege the visitors goal before Jackson had the home fans in raptures after rising to head in Southall's cross at the far post.