Ludwig Augustinsson and Andreas Granqvist, with a penalty, put Sweden in control before Edson Alvarez’s own goal made certain of the Scandinavians’ place in the second round.
Mexico joined them despite the defeat as Germany finished bottom thanks to a shock 2-0 defeat by South Korea in Kazan.
The 2014 champions were dumped out, with Janne Andersson’s Sweden the big winners having started the day in third place.
It was a dismal game in Ekaterinburg for Mexico but they survived to make the last 16.
The game had a historic start with Jesus Gallardo collecting the fastest booking since World Cup records began, pulled up after only 13 seconds for a foul on Ola Toivonen.
It continued to be lively as Emil Forsberg tested Guillermo Ochoa with a sixth-minute free-kick after the goalkeeper handled outside the box, before Marcus Berg’s acrobatic effort dropped wide.
Carlos Vela curled off target for Mexico after 17 minutes but they struggled to make an impact.
Sweden had penalty claims turned down after referee Nestor Pitana referred to VAR when the ball struck Javier Hernandez high on the arm after half an hour.
Pitana was told to take another look at it by the video referee but, despite replays showing the ball hitting Hernandez’s arm, he waved away Sweden’s protests.
A minute later Ochoa turned Berg’s instinctive volley over with Andersson’s side on top.
Sweden lost to a last-minute goal against ten-man Germany at the weekend, leaving them needing to better the champions’ result yesterday, and against most expectations a goal rush by the Swedes transformed the group order.
Sweden opened the scoring five minutes after the break thanks to Augustinsson’s first international goal. Viktor Claesson missed his kick in front of goal and the ball looped to Augustinsson to crack in a volley at the far post.
It allowed Sweden to relax and they doubled their lead after 62 minutes through Granqvist’s second spot-kick of the tournament.
Hector Moreno brought down Berg in the area and captain Granqvist stepped up to drive the ball high into the net.
Twelve minutes later it was 3-0 as Alvarez scored a calamitous own goal when he sliced the ball on to his standing leg from six yards and it bounced beyond Ochoa.
Hernandez missed a chance to pull a goal back soon after but Mexico were reliant on South Korea’s result against Germany.
They faced an anxious wait to hear the result from Kazan while playing out four minutes of stoppage time in their own game.
But news slowly filtered through of Kim Young-gwon and Son Heung-min’s goals for South Korea in injury time which started the Mexican party as Germany went home, bottom of Group F.