Germany became the fourth defending champion in the last five tournaments to be eliminated in the group stage. The four-time champions conceded a pair of injury-time goals while knowing a 1-0 victory would have been enough to advance because of the result in Sweden vs Mexico match.
El Tri were a bit off colour as Sweden looked dangerous in a match played out evenly between the two teams. Mexico survived a scare as a Germany win would have sent them out of Russia.
Mexico will face the Group E winner on July 2 at the Samara stadium while Sweden will face the Group E runners-up on July 3 at Saint Petersberg.
Second-half goals from defenders Ludwig Augustinsson and Andreas Granqvist, from the penalty spot, plus a comical own goal saw Sweden win 3-0 after a physical display from them, who made it to Russia by beating Italy in the playoffs.
Both the teams were ragged throughout the play time and will next play whichever two teams emerge out of Brazil, Switzerland and Serbia in Group E, which concludes later on Wednesday.
Mexico came into the game at the Yekaterinburg Arena in the driving seat of a group where all four teams, including South Korea, had a chance to qualify, but they appeared more nervous half-way down the line.
It took them less than 15 seconds to make their mark on Sweden, Jesus Gallardo earning the fastest yellow card of World Cup after clattering into Ola Toivonen almost straight from kick-off.
Goalkeeper Guillermo Ochoa handled outside the box and then flapped at the resulting free-kick. On 12 minutes Marcus Berg clipped wide from close range with a bicycle kick.
On the quarter-hour mark, Mexico, who had the overwhelming support inside the stadium, should have taken the lead when Carlos Vela found himself in space on the edge of the box.
His bending left-footer shaved the post, catapulting a few sombreros off the heads of Mexico fans as they expectantly sprang to their feet.
They were up again on 30 minutes when referee Nestor Pitana of Argentina took a look at a video replay when the ball hit Mexico striker Javier Hernandez on the arm in the box.
After lingering at the pitch-side monitor, then going back for a second glance, Pitana ruled it no penalty -- the Mexicans celebrated as if they had scored a goal.
The Swedish players and coach Janne Andersson were furious, and understandably -- Hernandez appeared to move his arm towards the ball. Sweden, with their greater physicality, were on top, but Mexico were dangerous on the break.
At the interval, with the game also goalless in Kazan, Mexico and Germany were going through.
Sweden badly needed a goal and five minutes after half-time they deservedly got it, defender Augustinsson popping up from left-back to volley in.
There was more drama on the hour, as Hector Moreno brought down Berg in the box and skipper Granqvist held his nerve in front of the massed ranks of Mexicans to bury his penalty.
Plastic beer cups rained down, before Mexico defender Edson Alvarez compounded a miserable evening with a 74th-minute own goal when he miskicked past Ochoa.
That left Mexico in a perilous position, as a late German winner would have sent the Central Americans crashing out, but two injury-time South Korean goals in Kazan ensured Juan Carlos Osorio's men edged through as runners-up.
Mexico will need to pick themselves up quickly if they are to go beyond the last 16 for the first time since 1986 and end a run of six consecutive second-round defeats.
Here are the Live updates:
Min 90+4: Final whistle! Denmark reach the knockout stages with a comfortable win as Mexico waits for the Germany vs South Korea result in order to get their ticket to knockouts.
Min 88: Lustig is booked by the referee after a rash foul on Lozano. Mexico wins a free kick on the edge of the Mexico box.
Min 85: Layun (Mexico) is booked by the referee.
Min 80: Albin Ekdal (in) comes off for Oscar Hiljemark (out) (Sweden)
Min 75: Jesus Corona (in) comes in for Andres Guardado (out) (Mexico)
Min 74: Mexico concedes another through an own goal from their right-back Alvarez!!!! Sweden is on a roll with three goals.
Min 68: Kiese Thelin (in) comes off the bench to replace Berg (out) (Sweden)
Min 65: M. Fabian (in) comes off the bench to replace J Gallardo (out) (Mexico)
Min 60: GOAL!!!! Berg (Sweden) is fouled inside the box by h. Moreno (Mexico). A penalty is awarded to Sweden. Hector Moreno gets a yellow. Granqvist scores from the spot. They seem to be on their way to Group 16. If the scoreline remains the same, Germany will be on their way home as their match against South Korea stays goalless.
Min 50: GOAL!!!! Sweden’s left-back Ludwig Augustinsson scores his first ever goal for his country, rampaging up the field to volley home from a tight angle after connecting with Viktor Claesson's cross from the right
Half-time report: A goalless but entertaining half came to an end as both Mexico and Sweden failed to find the back of the net despite some genuine chances in both the ends. Meanwhile, in Kazan, a boring and lacklustre Germany failed to score against South Korea. The Asian side also created some chances for themselves but failed to take the much-needed lead against the World Cup champions.
Min 45: It has been a fiery end-to-end encounter so far. Granqvist cuts off Lozano as Gallardo blasts a square ball across the face of the Swedish goal and injures himself in the process, the encounter is getting physical with both the sides trying to break the deadlock.
Min 38: Carlos Vela sees his shot sail over the bar. Mexico's players weaved a beautiful play into the attacking-thrid of Sweden but the deadlock stays.
Min 29: The resulting corner is fumbled up by the Mexico defence and a Sweden shot is frantically saved by Ochoa for a corner.
Min 28: Play is stopped after the referee has called for VAR consulting to check a potential handball by Javier Hernandez in the Mexico box. The Penalty shout was cancelled and corner given to Sweden.
Min 26: Larson of Sweden is booked due to a half-baked foul on Lozano
Min 25: A dangerous Sweden cross comes floating from the left flank but is saved by Ochoa before the Swedes could head it in.
Min 20: Although Mexico control maximum possession, Sweden look more dangerous on the attacks. Except for a few instances, Mexico is struggling to get a foot-hold on the attacking-third.
Min 16: A Genuine chance for Mexico. Carlos Vela's shot goes just wide of the left post!
Trivia: Jesus Gallardo's yellow card after 13 seconds is the fastest yellow ever shown in World Cup history.
Min 8: Sweden has started the game on the right foot, pressing the play from early on. The Swedes have missed a good opportunity when they won a free-kick on the edge of the box.
Min 1: Inside 13 seconds, Jesus Gallardo (Mexico) is booked for a lunging foul on Toivonen
Mexico vs Sweden: Match Preview
Ochoa; Gallardo, Moreno, Ayala, Salcedo; Guardado, Herrera; Lozano, Vela, Layun; Hernandez