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 have spent two seasons in the Championship after winning promotion from League One via the play-offs in 2010-11
They lost their first seven games of the season but are relegated despite winning four of their final nine matches and suffering only two defeats in their last 13 games
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.
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.
Peterborough fans react to Crystal Palace's winning goal, which sent Posh down
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.
Crystal Palace's play-off dates
Friday 10 May:
Crystal Palace v Brighton (19:45 BST)
Monday 13 May:
Brighton v Crystal Palace (19:45 BST)
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."
Full Time The game is over, as the referee blows his whistle.
Stephen Dobbie produces a right-footed shot from just outside the area that goes harmlessly over the target.
Unfair challenge on Jonathan Williams by Lee Tomlin results in a free kick. Free kick taken by Mile Jedinak.
Grant McCann restarts play with the free kick.
90:00 +1:09 Substitution
Substitution Jack Payne leaves the field to be replaced by Tyrone Barnett.
90:00 +1:09 Booking
Booking Booking for Glenn Murray.
Glenn Murray gives away a free kick for an unfair challenge on Craig Alcock.
Assist on the goal came from Stephen Dobbie.
88:58 Goal scored
Goal! - Mile Jedinak - C Palace 3 - 2 Peterboro Mile Jedinak scores a headed goal from inside the penalty box. Crystal Palace 3-2 Peterborough.
Centre by Stephen Dobbie,
Booking Dwight Gayle is booked.
Dwight Gayle gives away a free kick for an unfair challenge on Jonathan Williams.
Substitution Alex Pritchard leaves the field to be replaced by Nathaniel Knight-Percival.
Free kick awarded for an unfair challenge on Michael Bostwick by Glenn Murray. Craig Alcock takes the direct free kick.
The assist for the goal came from Mile Jedinak.
82:12 Goal scored
Goal! - Kevin Phillips - C Palace 2 - 2 Peterboro Kevin Phillips finds the net with a goal from the edge of the area to the top left corner of the goal. Crystal Palace 2-2 Peterborough.
Gabriel Zakuani gives away a free kick for an unfair challenge on Glenn Murray. The free kick is delivered right-footed by Stephen Dobbie from right channel, clearance made by Grant McCann.
Stephen Dobbie takes a shot. Robert Olejnik makes a save.
Close range shot by Glenn Murray goes over the bar.
The offside flag is raised against Glenn Murray. Robert Olejnik takes the indirect free kick.
Outswinging corner taken by Stephen Dobbie from the right by-line.
The ball is delivered by Dean Moxey, save made by Robert Olejnik.
Julian Speroni restarts play with the free kick.
Substitution Stephen Dobbie comes on in place of Kagisho Dikgacoi.
Lee Tomlin is flagged offside by the assistant referee.
Grant McCann shoots direct from the free kick, save by Julian Speroni. Inswinging corner taken from the left by-line by Alex Pritchard.
Booking Mile Jedinak receives a caution for unsporting behaviour.
Free kick awarded for an unfair challenge on Nathaniel Mendez-Laing by Mile Jedinak.
The assistant referee flags for offside against Kevin Phillips. Robert Olejnik takes the free kick.
Substitution Yannick Bolasie goes off and Jonathan Williams comes on.
Substitution Owen Garvan leaves the field to be replaced by Kevin Phillips.
Glenn Murray concedes a free kick for a foul on Grant McCann. Craig Alcock takes the free kick.
Lee Tomlin produces a right-footed shot from just outside the area that goes wide left of the goal.
Assist on the goal came from Alex Pritchard.
62:52 Goal scored
Goal! - Nathaniel Mendez-Laing - C Palace 1 - 2 Peterboro Nathaniel Mendez-Laing grabs a goal from just outside the penalty area to the bottom right corner of the goal. Crystal Palace 1-2 Peterborough.
Corner from right by-line taken by Owen Garvan.
Wilfried Zaha takes a shot. Save by Robert Olejnik.
Glenn Murray takes a shot. Blocked by Michael Bostwick.
Foul by Dean Moxey on Nathaniel Mendez-Laing, free kick awarded. Grant McCann crosses the ball in from the free kick, clearance by Damien Delaney. Inswinging corner taken left-footed by Grant McCann, clearance by Glenn Murray.
Owen Garvan produces a left-footed shot from just outside the box that misses to the right of the target.
Mile Jedinak gives away a free kick for an unfair challenge on Lee Tomlin. Grant McCann shoots direct from the free kick, Julian Speroni makes a save.
Corner taken by Lee Tomlin.
Free kick awarded for a foul by Damien Delaney on Dwight Gayle. Strike on goal comes in from Michael Bostwick from the free kick.
The referee blows his whistle to start the second half.
45:00 +2:59 Half time
Half Time The whistle is blown to end the first half.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.