Switzerland tied 2-2 with Costa Rica thanks to a bizarre late penalty, but thanks to Brazil's 2-0 victory against Serbia, Vladimir Petkovic's side secured its place in the knockout stage as runner-up of Group E.
Switzerland's reward is a date with surprise group winner Sweden, while Brazil will face Mexico on the other half of the bracket.
Switzerland entered the day level on points with Brazil and one clear of Serbia, so as long as the Seleção took care of business, its place in the round of 16 wouldn't be in jeopardy. That's how the day played out, with Switzerland provisionally going into first place in the group before Brazil got on the board.
The match started with a chance for Switzerland as Xherdan Shaqiri, who provided the late drama against Serbia, took an early free kick and found Mario Gavranovic, but he could just not meet the pass. Once again, Switzerland threatened in the box but Costa Rica did enough to protect Keylor Navas.
Costa Rica was also extremely aggressive too as Joel Campbell was making the most of his start by attacking from the right. His shot was saved by Yann Sommer, the warning signs were there. Celso Borges also tested the Swiss stopper, but once again he stood firm.
Once again, the Central American side threatened by hitting the crossbar. It was a fantastic attempt from Daniel Colindres that had beaten Sommer, but didn't dip in time. In the 16th minute, Gavranovic found some space for Switzerland just outside the box, but his curling shot went wide of Keylor Navas's goal.
Colindres threatened again in the 23rd minute as another Costa Rican counter found the speedster from Saprissa open on the left wing, but as he entered the box, the Swiss defense blocked his shot. It was clear his pace was causing all sorts of problems against Vladimir Petkovic's side.
In the 30th minute, Switzerland broke the deadlock thanks to Stephan Lichtsteiner's cross, which was met by Blerim Dzemaili and made it 1-0.
In the 37th minute, Costa Rica had a great chance thanks to free kick in the box, but no player could meet the cross. Another great chance went begging as Colindres couldn't properly find Bryan Ruiz in the box. Despite the scoreline, it was clear Los Ticos were creating chances.
The first half ended with plenty of chances for both sides, with Switzerland looking good for its lead thanks to some stellar work from Lichsteiner, who had been extremely productive at both sides of the pitch.
In the 47th minute, captain Kendall Watson's great dummy gave Campbell a great chance inside the box, but the Real Betis man didn't expect it in time, and so he sent it wide. But again, Oscar Ramirez's side was threatening to equalize.
In the 54th minute, Campbell turned provider once again as a great ball in the box found Colindres, who headed it to Bryan Oviedo, but another chance was wasted. But moments later, Waston powered in a fantastic header to tie it 1-1. A well deserved goal that still meant the Swiss stayed in second place.
With 20 minutes left, Switzerland was looking for the lead by building possession entering the final third, but Costa Rica stood firm, protecting Navas and keeping it 1-1. In the 76th minute, Breel Embolo crossed in a great ball for Josip Drmic, but his header hit the upright.
Finally, the same combination worked out as Embolo once again fed Drmic, and this time, he made it count. Switzerland led 2-1.
VAR came into place late in the game as it denied a penalty appeal for Costa Rica. But another penalty came moments later and this one was approved. Ruiz took it, it hit the crossbar, and then bounced onto Sommer's head and went in. The penalty went down as a Sommer own goal, forcing the 2-2 draw.
In the end, the result didn't matter as Brazil's 2-0 victory against Serbia had secured Switzerland's place in the knockout stage, where it will face Sweden in the round of 16.
You can read our recap from the Group E finale between Brazil and Serbia here.
Here were the lineups for both teams:
Here are the rosters for both sides:
Goalkeepers: Roman Burki (Borussia Dortmund), Yvon Mvogo (RB Leipzig), Yann Sommer (Borussia Monchengladbach)
Defenders: Manuel Akanji (Borussia Dortmund), Johan Djourou (Antalyaspor), Nico Elvedi (Borussia Monchengladbach), Michael Lang (FC Basel), Stephan Lichtsteiner (Juventus), Jacques-Francois Moubandje (Toulouse), Ricardo Rodriguez (AC Milan), Fabian Schar (Deportivo La Coruna)
Midfielders: Valon Behrami (Udinese), Blerim Dzemaili (Bologna), Gelson Fernandes (Eintracht Frankfurt), Remo Freuler (Atalanta), Xherdan Shaqiri (Stoke City), Granit Xhaka (Arsenal), Steven Zuber (Hoffenheim), Denis Zakaria (Borussia Monchengladbach)
Forwards: Josip Drmic (Borussia Monchengladbach), Breel Embolo (Schalke), Mario Gavranovic (Dinamo Zagreb), Haris Seferovic (Benfica)
Manager: Vladimir Petkovic
Goalkeepers: Leonel Moreira (Herediano), Keylor Navas (Real Madrid), Patrick Pemberton (LDA)
Defenders: Johnny Acosta (Aguilas Dorados), Francisco Calvo (Minnesota United), Oscar Duarte (Espanyol), Cristian Gamboa (Celtic), Giancarlo Gonzalez (Bologna), Ronald Matarrita (NYCFC), Bryan Oviedo (Sunderland), Ian Smith (Norrkoping), Kendall Waston (Vancouver Whitecaps)
Midfielders: Randall Azofeifa (Herediano), Christian Bolanos (Saprissa), Celso Borges (Deportivo La Coruna), Daniel Colindres (Saprissa), David Guzman (Portland Timbers), Bryan Ruiz (Sporting Lisbon), Yeltsin Tejeda (Lausanne), Rodney Wallace (NYCFC)
Forwards: Joel Campbell (Real Betis), Marco Urena (LAFC), Johan Venegas (Saprissa)
Manager: Oscar Ramirez