The Swedes went through to the knockout rounds as the unlikely winners of Group F, taking the beaten Mexicans with them.
Second-half goals from defenders Ludwig Augustinsson and Andreas Granqvist, from the penalty spot, plus a comical own goal were reward for a physical display from Sweden, who only made it to Russia by beating Italy in the play-offs.
They and Mexico, who were ragged throughout, 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 in a group where all four teams, including South Korea, could qualify, but they appeared the more nervous.
It took them less than 30 seconds to make their mark on Sweden, Jesus Gallardo earning a yellow card for 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.
Download our app and read this and other great stories on the move. Available for Android and iOS.