With Argentina beating Poland 2-0 in Group C at Stadium 974 and Mexico leading by the same scoreline at Lusail Stadium in the 94th minute, Gerardo Martino's side needed one more goal to prevent them from going out according to Fifa's fair play rule.
But Salem Al Dawsari's injury-time consolation goal for Saudi Arabia sent the distraught Mexicans home anyway, leaving the Polish players to celebrate across the city.
After a stop-start first half with little rhythm to it, the match burst into life two minutes after the break as Henry Martin reacted quickest to finish Cesar Montes' flick on.
Mexico fans had barely calmed their celebrations before Luis Chavez curled a glorious 30-yard free-kick into the top corner to send their supporters into pandemonium.
They thought they had another but Hirving Lozano's low strike was chalked off for offside and Martin should have had a second but volleyed over from 12 yards out.
It was all Mexico as they pushed for further goals. Chavez nearly struck another stunning free-kick but Mohammed Al Owais palmed it out, then Orbelín Pineda curled narrowly wide.
Mexico thought they had sealed progression when Uriel Antuna stroked in late on, but that too was flagged for offside and they went out in the most excruciating manner.
The first half was one to forget, the drama in the second will live long in the memory.
It was absolute chaos before the game, with thousands of Saudi fans making the trip across the border for a game in which victory would have secured a place in the knockout stage.
But they never really managed to put Mexico under pressure and ultimately both sides went out of the competition.
At one stage, Mexico were set to be knocked out because they had collected more yellow cards (seven) than Poland (five), with the two teams level on points and goal difference.
While Al Dawsari's late goal changed little, Mexico fans were still left dejected at full-time, with some staying long after the final whistle with their head in their hands.
Martin's opener from close range and Chavez's peach of a free-kick gave them hope, but two disallowed goals crushed any optimism of advancing out of the group.
The result ends a sequence of progression to the round of 16, having done so at the previous eight World Cups, a run stretching back to 1978.
For Saudi Arabia, the tournament ends in disappointment by finishing bottom of the group despite an opening game victory over Argentina that will go down as one of the biggest shocks in World Cup history.
Mohamed Kanno had his chances, shanking wide from the angle and curling a free-kick narrowly over, while Ali Al Hassan almost headed in on the stroke of half-time.
The stadium was filled mostly with raucous Saudi supporters and they went home with one final cheer as Al Dawsari coolly converted late on.
-- Courtesy of BBC Sport