Fifa World CupHost nation: Qatar Dates: 20 November-18 December Coverage: Live on BBC TV, BBC iPlayer, BBC Radio 5 Live, BBC Radio Wales, BBC Radio Cymru, BBC Sounds and the BBC Sport website and app. Day-by-day TV listings - Full coverage details
The final games in Group C will go down as one of the most dramatic evenings at any World Cup.
Poland had just lost 2-0 against Argentina, which guaranteed the South American side top spot in Group C, but then came the incredible permutations.
With Mexico winning by the same scoreline against Saudi Arabia, Poland and Mexico were level on points, goals scored, conceded and goal difference.
Gerardo Martino's Mexico were staring at the prospect of going out on Fifa's fair play rule having collected seven yellow cards to Poland's five, and becoming just the second side after Senegal four years ago to exit by that metric.
With seven minutes of time added on, a goal Mexico would have completed the swing required for an unlikely progression into the round of 16.
But Salem Al Dawsari's injury-time consolation strike ended any hope of a remarkable Mexico revival having failed to win their opening two games.
"I am the man responsible for this frustration and disappointment. It is real sadness and I assume all the responsibility for this huge failure," said Argentine Martino, who will leave his role after the tournament.
"We outplayed the opponent created more chances and we could have scored as many goals as needed.
"But we failed and I don't think we were knocked out in the previous matches, rather today."
Mexico exit at the group stage for the first time since 1978, ending a run of eight straight occasions of advancing to the knockout stage, while Poland's reward is a meeting with defending champions France on Sunday (15:00 GMT).
- 'This was the night the real Argentina stood up'Fifa World Cup: Groups and scheduleWorld Cup TV coverage guide
'We knew we were going out'
The game at Stadium 974 had finished 2-0 and the one at Lusail Stadium - just 18 miles away - was the same scoreline, with play still going on.
Asked by BBC Sport if he knew the result of the other game, Martino said: "Yes. That is why we went for the third goal and we added two centre-forwards together.
"The moment we knew that Poland had fewer yellows [than us], we were going out and it was necessary in that moment.
"We knew the result of Argentina winning 2-0 and we assessed that unless many players were booked in the Poland game, the results meant we were still out."
Poland boss Czeslaw Michniewicz said: "We had an agreement that only me and the technical staff would be across what was happening in the Mexico match.
"My heart stopped a bit when Grzegorz Krychowiak was booked, we knew it was minus three points and fair play was already taken into consideration.
"So Piotr Zielinski was supposed to leave but we substituted Krychowiak because for five minutes we didn't have control of the game and couldn't make the substitution, while he was endangered by the second yellow card.
"My technical staff told me there was only a difference of two or three yellow cards so it was very close, we were at a difficult time when it was 2-0.
"The mistake of Krychowiak could have made us go back to the hotel and pack but now I hear we are going to play France.
"I told them to avoid stupid cards, we were doing anything to avoid any provocation, avoid talking to referees."
'Madness - such a good game of football'
The lead-up to the game was all about nervous Saudi Arabia fans filling the metro, the boulevard and the stadium in the hope of seeing a victory that would take them through to the knockout stage.
One local journalist said around 150,000 supporters had travelled across the border for the game, with more than 60,000 estimated to be at the game, heavily outnumbering the Mexicans.
"Inshallah" (God willing) was the word on every supporter's lips but at the final whistle it was both sets of fans in green who went home disappointed.
Saudi finished bottom of the group with one victory - and what a win it was against Argentina - while Mexico's goalless draw against Poland and loss to Lionel Scaloni's side proved costly.
Henry Martin's goal from close range and Luis Chavez's stunning free-kick set their small number of fans alight, and the Mariachi band behind the goal did its best to suck the ball into the net one more time.
Mexico players embraced each other at the final whistle knowing their journey was over, while supporters sat in their seats long after the final whistle with some in tears of disbelief.
"It's been a crazy day of football with the twists and turns you get at this stage of the competition," said Gary Lineker on BBC One.
Former Wales defender Ashley Williams said: "Madness. That was such a good game of football regardless of who went through."
Tears of celebration for Poland
BBC Sport's Andy Cryer at Stadium 974
From the moment Alex Mac Allister put a dominant Argentina ahead early in the second half against Poland, there was never any doubt about who was winning the game.
The doubt was whether Poland would survive and for most of the second half the players on the pitch looked fairly unconcerned. There was no racing forward looking for a goal, they were simply hanging on and hoping they did not lose by more than the eventual 2-0 scoreline.
While the players may have been unaware of the drama being played out, the small pocket of Poland fans - who were drowned out by the incredible Argentine support - certainly weren't.
Most of them were spending more time on their phones checking events at Mexico v Saudi Arabia than they were watching their own - fairly hapless - side.
Heads went into hands from many of the Poles as Mexico scored their second and, when Argentina deservedly made it 2-0, they knew they were surviving in the tournament purely on yellow cards.
Argentina were inches from scoring in injury time, to knock Poland out, but the final whistle was not the end of the agony for Poland.
The players and staff gathered, the fans were glued to their screens, and then a cheer. Saudi Arabia had pulled a goal back against Mexico in stoppage time, Poland were now safe on goal difference rather than fair play.
A goal for Mexico would still have knocked Poland out though, guaranteeing a couple more minutes of pain before the final whistle at Lusail was greeted by emotional scenes at the 974.
Tearful fans hugged each other, some players charged around in celebration, while others sank to the floor in the knowledge they had got very, very lucky.
Get the latest results and goal notifications for any team at the Fifa World Cup by downloading the BBC Sport app: Appleexternal-link - Androidexternal-link - Amazonexternal-link
Get your daily dose of Fifa World Cup reaction, debate & analysis with World Cup Daily on BBC Sounds