Match ends, Brentford 1, Barnsley 2.
Barnsley scored an injury-time winner at Brentford to survive relegation from the Championship - a result which consigned the Bees to a place in the play-offs.
However, the Tykes could yet find themselves in League One next season if Wigan's appeal against their 12-point deduction for entering administration is successful.
Callum Styles put the visitors ahead just before the break, but Josh Dasilva's curling effort with 17 minutes left gave the Bees hope of a place in the top two and kept Barnsley in the bottom three.
But Clarke Oduor turned in Patrick Schmidt's low cross in the first minute of second-half stoppage time to spark jubilant celebrations from the visitors.
The win, coupled with Charlton's defeat at Leeds and Wigan's draw against Fulham means the Oakwell outfit, who were bottom and seven points from safety when the season restarted, are set to avoid an immediate return to the third tier.
Brentford needed four points from their final two games of the season to finish second after West Brom lost at Huddersfield on Friday, but back-to-back defeats mean Thomas Frank's side will now face Swansea in the play-off semi-finals as they bid to reach the top flight for the first time in 73 years.
Struber inspires Tykes' great escape
Barnsley's survival vindicates the club's decision to part company with Daniel Stendel in October, replacing him with Gerhard Struber, from Austrian side Wolfsberger AC, the following month.
The Tykes had been in the relegation zone since 14 September, but seven points from their first three games after the season restarted in June gave the club hope of survival.
A late win at Nottingham Forest on Sunday took their battle to survive into the final day, and Barnsley repeated the feat after weathering an onslaught from the Bees following Dasilva's equaliser.
They move up to 21st in the table, leapfrogging Charlton and going above Wigan after their deduction was imposed by the English Football League, yet Barnsley face a wait off the pitch to see if the Latics are successful in their appeal against their 12-point sanction.
Bees unable to take advantage of West Brom draw
With West Brom held in their match at home to QPR, a win would have been enough to see the Bees pip the Baggies to the second automatic promotion place.
Brentford were the Championship's leading scorers heading into the game, but at times struggled to create chances against a tenacious Barnsley side who pressed well and proved hard to break down.
Said Benrahma had their first real opening, having a low shot saved by Jack Walton, and Styles showed composure to put the Tykes ahead four minutes before the break with a low shot after Pontus Jansson failed to properly clear a cross.
Walton kept his side ahead with a brilliant save on the stroke of half-time after Bryan Mbeumo met Rico Henry's cut-back, clawing his shot onto the post before Benrahma had a follow-up effort blocked.
The visitors could have extended their lead after the break when Jacob Brown had a shot blocked following a quick break and Marcel Ritzmaier had a strong claim for a penalty turned down.
Dasilva's curling effort sparked greater urgency as the Bees chased a winner.
Ollie Watkins spurned a glorious chance as he failed to find the target when picked out by Benrahma with 10 minutes remaining, and Tariqe Fosu slipped when trying to latch on to a cut-back from Watkins.
And the Bees were caught out and had their automatic promotion dream ended when an unmarked Oduor tapped in.
Brentford had won eight successive games to give themselves a shot at the top two, but consecutive defeats mean they will have to travel to Wembley Stadium next month and win the play-offs to reach the Premier League for the first time.
The Bees go to Swansea for the first leg of their semi-final on Sunday (18:30 BST), and they will host a final match at Griffin Park, the west London club's home of 116 years, for the second leg on Wednesday.
The Bees could yet move into their new stadium near Kew Bridge station this summer as a top-flight outfit, but will need to regroup after a damaging couple of results.
Brentford boss Thomas Frank:
"Football is 80% suffering and 10% joy. When you have that opportunity to go direct to the Premier League, that is the emotional part of it - but Barnsley did a good job.
"Right now everything is tough but this is football and this is life. This was not straightforward.
"We can't sit down and cry too long because that will never help anything. We need to go again and pick ourselves up. We will have a sleep and then be ready to fight again."
Barnsley boss Gerhard Struber:
"We are safe! I think eight weeks ago I told my boys we need everyone in the squad and I think today Callum and Clarky scored the most important goals of the season.
"Now we are very happy with a big result, we are staying in the league. Now it's time to celebrate, have a big party, and not speak about the future. We are in the here and now. It's party time."
- 18JanssonBooked at 85mins
- 5PinnockSubstituted forFosu-Henryat 70'minutesBooked at 88mins
- 3HenrySubstituted forJensenat 60'minutes
- 17MarcondesSubstituted forCanósat 70'minutes
- 6NørgaardBooked at 79mins
- 19MbeumoSubstituted forBaptisteat 86'minutes
- 16Valencia Castillo
- 18SollbauerBooked at 75mins
- 2WilliamsBooked at 55mins
- 14LudewigSubstituted forSchmidtat 89'minutes
- 17RitzmaierSubstituted forOduorat 86'minutesBooked at 90mins
- 16ThomasSubstituted forSimoes Inacioat 56'minutes
- 11ChaplinSubstituted forWoodrowat 86'minutes
- 28Simoes Inacio
- Robert Jones
- Shots on Target
Second Half ends, Brentford 1, Barnsley 2.
Foul by Henrik Dalsgaard (Brentford).
Michael Sollbauer (Barnsley) wins a free kick in the defensive half.
Christian Nørgaard (Brentford) wins a free kick in the defensive half.
Foul by Alex Mowatt (Barnsley).
Attempt missed. Sergi Canós (Brentford) left footed shot from outside the box is high and wide to the left.
Attempt blocked. Ollie Watkins (Brentford) left footed shot from the centre of the box is blocked. Assisted by Pontus Jansson with a headed pass.
Corner, Brentford. Conceded by Jordan Williams.
Clarke Oduor (Barnsley) is shown the yellow card.
Saïd Benrahma (Brentford) wins a free kick in the attacking half.
Foul by Elliot Simoes (Barnsley).
Goal! Brentford 1, Barnsley 2. Clarke Oduor (Barnsley) left footed shot from very close range to the centre of the goal. Assisted by Patrick Schmidt.
Henrik Dalsgaard (Brentford) wins a free kick in the defensive half.
Foul by Jordan Williams (Barnsley).
Attempt saved. Callum Styles (Barnsley) left footed shot from outside the box is saved in the bottom right corner. Assisted by Cauley Woodrow.
Substitution, Barnsley. Patrick Schmidt replaces Kilian Ludewig.
Tariqe Fosu-Henry (Brentford) is shown the yellow card for a bad foul.
Foul by Tariqe Fosu-Henry (Brentford).
Alex Mowatt (Barnsley) wins a free kick in the defensive half.