Paul Parry scored the goal that keeps Cardiff City up, but puts Gillingham on a relegation knife-edge with just one game remaining.
Parry equalised with just five minutes to go, directing a deft flicked header into the roof of the net from Neal Ardley's cross into the box.
Until that point it looked as if Gillingham would secure their Championship status thanks to Matthew Jarvis' 75th minute goal.
The Gillingham sub raced on to Paul Smith's through ball and fired his shot towards goal. Keeper Neil Alexander got a touch and defender Chris Barker attempted to hack clear off the line, but the strong challenge of Nicky Southall ensured that everyone ended up in the back of the net - crucially along with the ball.
But Parry's late effort means Gillingham are not yet safe and they go to Nottingham Forest next weekend still in danger of the drop.
Until the late drama it had been a tense affair with flair football at a premium - perhaps unsurprisingly with so much at stake near the bottom.
Chances were few and far between in a dismal first-half in which neither team seemed keen to gamble on pushing men forward.
Cardiff perhaps felt a point would be enough to guarantee their safety, while the Gills failed to break down a resolute Bluebirds defence.
Indeed, the best opportunity came when Gillingham full-back James McEveley almost headed into his own net after nine minutes.
Ardley's free-kick was swung into the danger area and although McEveley did well to win the aerial challenge, he only succeeded in directing the ball backwards and was fortunate his header smacked against the bar and bounced away to safety.
Apart from that it was pretty mundane stuff, although Gills striker Mamady Sidibe should have done better when he beat keeper Alexander to Southall's throughball, but could only prod his shot wide of the target.
Alexander was also forced into action to tip over Darius Henderson's close-range header as the home side edged possession, but could not find a way through.
And even when they did in the second half, they were then pegged back and they could still be playing in League One next season if results go against them next weekend.