Switzerland booked their place in the last 16 despite being held to a late 2-2 draw by Costa Rica in their final game of Group E.
The Swiss went into the game needing a draw to qualify for the knockout rounds and Blerim Dzemaili fired Switzerland ahead in the 31st minute before Costa Rica scored their first goal of the 2018 World Cup as Kendall Waston's header got them level.
There was more late drama at the Nizhny Novgorod Stadium as Josip Drmic swept Switzerland back into the lead with two minutes of normal time to play.
Switzerland: Sommer (7), Rodriguez (7), Akanji (6), Schar (6), Lichtsteiner (7), Xhaka (7), Behrami (6), Embolo (8), Dzemaili (6), Shaqiri (7), Gavranovic (6).
Subs used: Zakaria (6), Drmic (6), Lang (4).
Costa Rica: Navas (7), Acosta (6), Gonzalez (7), Oviedo (7), Gamboa (7), Waston (7), Borges (7), Colindres (7), Ruiz (7), Guzman (7), Campbell (7).
Subs used: Wallace (4), Azofeifa (n/a), Smith (n/a).
Man of the match: Breel Embolo.
But after having a penalty appeal turned down following a VAR review in the 89th minute, Costa Rica were awarded a spot-kick two minutes later, with Bryan Ruiz's effort heading goalwards via the back of goalkeeper Yann Sommer, who takes the unwanted credit of an own goal.
Switzerland now head into the last 16 as the second place team in Group E, two points behind group winners Brazil, who beat Serbia 2-0 in Moscow. Costa Rica will head home with a point to show for their stay in Russia.
Costa Rica began strongly, and forced three saves from Sommer in the sixth minute. He initially saved a curling shot from Joel Campbell before Celso Borges powered a header towards goal that forced a leaping, fingertip save from the goalkeeper. Finally, Cristian Gamboa tried his luck, but his effort landed straight into Sommer's hands.
Switzerland made two changes with Breel Embolo and Mario Gavranovic coming into the XI. Costa Rica switched three players, with Kendall Watson, Daniel Colindres and Joel Campbell in the side.
Five minutes later, and it was another glorious chance missed for Costa Rica. Daniel Colindres muscled Xherdan Shaqiri off the ball on the left before taking a few strides to the top of the area to strike, but his effort hammered off the underside of the crossbar before going wide.
Switzerland grew into the half, and broke the deadlock just after the half-an-hour mark. Stephan Lichtsteiner - who will miss the round-of-16 game after picking up a second yellow card of the tournament, along with Fabian Schar - put a great cross into the area, which Breel Embolo headed down at the back post. The ball then fell to a waiting Dzemaili in the middle who fired home.
Costa Rica deserved to be level and got their reward - and their first goal of the 2018 World Cup - 11 minutes after the restart. Campbell delivered a corner and it was Waston who rose above the pack to bullet a header into the back of the net, sparking wild celebrations with the bench and in the stands.
The rest of the second half was a mostly quiet affair until Switzerland retook the lead in the 88th minute thanks to a fine Drmic strike. Denis Zakaria fired a lovely cross into the area after being picked out by Granit Xhaka, with the ball expertly swept home by the waiting Drmic.
But a minute later and Costa Rica were awarded a penalty by referee Clement Turpin after Bryan Ruiz was caught by Ricardo Rodriguez in the box. However, following a VAR review, Ruiz was found to be offside as Waston headed the ball on and the decision was reversed.
However, they were awarded another penalty in the first minute of four added on, with Zakaria fouled Campbell in the area and no VAR needed this time. Ruiz - who is expected to retire after the World Cup - stepped up but his effort hit the underside of the crossbar before coming off the body of Sommer and going into the back of the net, handing Costa Rica a deserved point.
- Switzerland have now progressed from the group stage in four of the last five World Cups in which they have appeared (also 1994, 2006 and 2014), only failing to do so at the 2010 tournament.
- Costa Rica have failed to win any of their matches in a particular World Cup tournament for only the second time, having last done so when they lost all three games in 2006.
- Kendall Waston's goal for Costa Rica was their first in 424 minutes of play in World Cup matches, with their previous goal scored by Bryan Ruiz in the Last 16 of the 2014 tournament against Greece.
- Defender Stephan Lichtsteiner became the first player to reach 10 World Cup appearances for Switzerland by starting this match.
Man of the match - Breel Embolo
After a year of injury heartbreak, Embolo had a fine performance in Nizhny Novgorod, setting up the opening goal for Dzemaili and producing a number of other knockdowns for his team-mates to score, although the final product was lacking somewhat.
He put a good case together for his inclusion in Switzerland's last 16 game against Sweden and was a bright spark for his country.
Switzerland will now play Sweden in their last 16 game on Tuesday 3 July in St Petersburg.