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.
“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.