Chesterfield were on the end of a smash and grab raid by Gillingham for the second time this season as their chances of avoiding relegation took a severe blow.
The Kent club had scored twice in stoppage time to beat Chesterfield back in November and they left it late once again to take the points and end a wretched run of away results.
Andrew Crofts' header came in the second minute of added time, leaving Chesterfield with no chance of a reply and they are now staring relegation in the face.
The Gills are safe and their manager Ronnie Jepson will have been pleased with the resilience shown by his players, who had not tasted victory away from home since late December.
Jepson opted for an extra man in midfield and his tactics paid off as the Spireites found it hard to break through despite having most of the possession.
When the chances did come, on-loan goalkeeper Romain Larrieu was in inspired form and twice he denied Jamie Ward.
The pacy striker broke through on goal in the 19th minute after riding a challenge from Nicky Southall, but Larrieu spread himself to block the low shot.
He repeated that save in the 48th minute when substitute Alex Bailey played the ball into Ward, whose first-time shot was brilliantly kept out by Larrieu at his near post.
Chesterfield continued to press but Gillingham showed they could also pose a threat when Danny Jackman struck a shot from 30 yards which Michael Jordan touched on to a post.
But the action was mostly at the other end and, as the home crowd became increasingly desperate, with news that their relegation rivals were all picking up points.
They had late chances. First Nicky Southall headed off the line from Paul Hall and then Ward turned and drove a shot against the underside of the bar in the 81st minute.
Hall headed the rebound towards goal but Larrieu threw himself across his line to push the ball to safety.
It was an outstanding save and he made another in the 90th minute when he kept out a shot from Paul Shaw with his feet as he was heading in the opposite direction.
If that was bad enough for Chesterfield worse followed when Gillingham plundered all three points with a classic breakaway.
Mark Bentley delivered an excellent cross from the right and Crofts ghosted in to head the goal that ensures his team will be in League One next season.