Mile Jedinak's 89th-minute header relegated Peterborough to League One and sealed a place in the Championship play-offs for Crystal Palace.
In a dramatic finish at Selhurst Park, Jedinak rose to meet a free-kick and head into the bottom corner.
A draw would have kept Posh up and they led twice through two solo goals from Lee Tomlin and Nathaniel Mendez-Laing.
But Palace replied through Glenn Murray's penalty and Kevin Phillips equalised before Jedinak struck.
It was a cruel end to an afternoon of topsy-turvy emotions for the visiting fans, who had not only celebrated their side's goals but also news of events from the John Smith's Stadium where fellow strugglers Huddersfield and Barnsley were playing.
Posh had started the day fourth from bottom, above Barnsley on goal difference and three points behind Huddersfield, and for long spells either the Terriers or Tykes were provisionally below them in the final relegation spot.
The game between the Yorkshire sides eventually finished 2-2, meaning a point would be enough for Posh. But though they led with seven minutes remaining, they were beaten at the death.
Their total of 54 points is the highest by a relegated team in Championship history.
Ultimately, results elsewhere meant Palace would have finished in the top six even if they had lost, but they came into the game needing victory to be certain.
With Peterborough also going all out for victory, the result was an enthralling encounter from the start.
Palace were the first to threaten, and it took an amazing goal-line clearance by Grant McCann to deny Murray, who followed up after Posh keeper Bobby Olejnik had parried Damien Delaney's point-blank header.
Under the cosh on the pitch early on, Posh were under more pressure once Barnsley took the lead at Huddersfield.
But things quickly got better for Darren Ferguson's side. After 28 minutes, Lee Tomlin picked up the ball on the left flank as Peterborough broke and beat five back-tracking Palace defenders before burying his shot past Julian Speroni.
Before half-time, however, they were back in trouble. Gabriel Zakuani made a needless and clumsy challenge on Danny Gabbidon from a corner and, from the resulting penalty, Murray stepped up to equalise. As things stood, Posh would again be in the bottom three.
But the real drama was yet to come. At first everything went Posh's way. A Huddersfield equaliser put Barnsley back in trouble, Tommy Rowe's superb goal-line clearance stopped Palace going ahead and Peterborough then took the lead when Mendez-Laing cut in from the right and beat Speroni with a bouncing shot.
Even Barnsley retaking the lead in West Yorkshire would not have sent Posh down and their position seemed even stronger when Huddersfield scored their second equaliser.
Now just a draw would be enough for Posh, but they wobbled in the 83rd minute when veteran Phillips came off the bench for Palace to squeeze home an equaliser with an expert finish into the bottom corner.
And the decisive moment came with a minute of normal time remaining. Stephen Dobbie swung over a free-kick and Jedinak found the net to end Peterborough's two-year stay in the Championship.
Meanwhile, Palace kept their promotion hopes alive. as their first win since 5 March sets up a play-off semi-final with arch-rivals Brighton next week.
Crystal Palace manager Ian Holloway:
"The way that game went is about as ridiculous as the last few weeks have been.
"What I'm finding out really quickly about this group is the confidence they have.
"We got back into the game twice when we needed to after we had got ourselves into a pickle and it worked.
"Kevin Phillips has been doing that all his career and hopefully he can help us. We've come through it now and I think we'll be stronger for that against Brighton."
Peterborough boss Darren Ferguson:
"To be so close to what we had set out to do and have it taken away from us in the last few seconds is the worst way it could have happened.
"We are all shattered. I had so much belief and confidence we would stay up. The only time I have thought we were down all season is now, when reality has sunk in that we are down.
"No words I can say now will make a difference. I really don't think we could have done any more with that group of players. It's just a bizarre season where 54 points sees us relegated. It's never happened before."