Match ends, Switzerland 2, Costa Rica 2.
Switzerland set up a World Cup last-16 tie with Sweden after a draw with Costa Rica that featured a bizarre late penalty drama.
Vladimir Petkovic's side kicked off needing just a point from their final Group E game to reach the knockout stages for the third time in four tournaments - but Serbia's 2-0 defeat by Brazil meant they would have gone through even with a defeat.
The Swiss, though, had to settle for second place in their group and just a point - despite substitute Josip Drmic putting them 2-1 ahead in the 88th minute.
Costa Rica, already eliminated, equalised in injury time, as Bryan Ruiz's penalty hit the bar and then bounced in off Swiss goalkeeper Yann Sommer's head for an own goal.
- How you rated the players
- Late penalty drama: How it unfolded
- Who still needs what? Group stage permutations
Switzerland had taken a first-half lead through Blerim Dzemaili, despite a bright start from Costa Rica, but Kendall Waston headed Los Ticos level 10 minutes into the second half from Joel Campbell's corner.
Shortly after Drmic scored, Costa Rica were awarded a penalty - only for the decision to be overturned by the video assistant referee system for an offside.
Campbell was then brought down in the box, with the penalty decision standing this time, allowing Costa Rica to equalise.
Group E winners Brazil will now face Mexico, after 2014 champions Germany failed to progress from the group stage for the first time since 1938.
Switzerland do enough
Despite the controversy surrounding the 'double-headed eagle' gestures made in their victory over Serbia last time out, key figures Granit Xhaka, Xherdan Shaqiri and captain Stephan Lichtsteiner were all available for Switzerland.
The trio avoided bans from Fifa this week, instead receiving fines for unsporting behaviour after making the gesture, which symbolises the Albanian flag.
It was Lichtsteiner's cross into the box after 31 minutes that allowed Dzemaili to provide the Swiss with the breakthrough they needed - striking powerfully beyond Keylor Navas in the Costa Rican goal.
Shaqiri too looked lively from the start as he tested the Costa Rican defence but the Swiss largely rode their luck early on as Daniel Colindres struck the underside of the crossbar.
Costa Rica offered warning signs that, despite having nothing to play for, they would not roll over, and their leveller made Petkovic's side nervy.
Because of Brazil's superior goal difference, Drmic's calm, side-footed finish beyond Navas would still not have been enough for Switzerland to top the group.
With Petkovic's side seemingly happy enough just to progress, the late leveller will likely not affect their outlook heading into the knockout stages.
Indeed, Switzerland, who reached the last 16 in Brazil four years ago, have now lost just once in their past 25 matches and finished a World Cup group unbeaten for the first time since the 2006.
Costa Rica out with a fight
Having achieved their best-ever World Cup result four years ago by reaching the quarter-finals, Costa Rica had failed to register a point or goal going into their final game of the tournament.
Having lost 1-0 to Serbia in their opening match, then conceded two injury-time goals to Brazil, Los Ticos threatened to come up short again in Nizhny Novgorod as they were punished for failing to convert a host of early chances.
Despite falling behind, Oscar Ramirez's side returned after the break with the same energy they began the game and were rewarded with their first goal in 424 minutes of play at the World Cup, as Waston scored.
Aware of the opposition's situation in the group, Costa Rica continued to harry Switzerland and set up a dramatic end fitting of their valiant though largely unrewarded efforts in Russia.
Despite conceding late on, Ramirez's men - determined not to finish empty-handed - took their final opportunity from the spot though it was not enough to lift them from the foot of the table.
Man of the Match - Joel Campbell (Costa Rica)
'We know we can do better' - what they said
Switzerland midfielder Blerim Dzemaili: "Costa Rica didn't surprise us. In the first two games, they had so many chances to score. We didn't think it was an easy game.
"We made it through the group stage again. We had a tough group. Every game was so tough and I think we can be proud.
"We are continuing to play well but we know we can do better. We can analyse everything then, in the next game, we can do better."
Costa Rica manager Oscar Ramírez: "Every single match is different and you need to think about the way you want to play. Unfortunately we didn't have the chance to go through.
"Our group is very tough. It is the fifth time we have been at the World Cup and I think this team has been one of the strongest.
"We could have had different results but that's the way football is. We will try to improve our results but we have played against tough teams - number two and number six in Fifa's rankings but still, we played well."
Sommer joins own goal list - the key stats
- Switzerland have progressed from the group stage in four of the past 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 at a World Cup finals for only the second time, having last done so when they lost all three games in 2006.
- Switzerland have avoided defeat in each of their three group stage matches at the World Cup for the first time since 2006.
- The Swiss have scored at least once in six of their past seven World Cup games, scoring 12 goals in those seven matches, as many as in their previous 16 combined.
- Kendall Waston's goal for Costa Rica was their first in 424 minutes of play in World Cup matches (not including penalty shootouts), with their previous goal scored by Bryan Ruiz in the last 16 of the 2014 tournament against Greece.
- Costa Rica became the last side to score a goal at this year's World Cup, doing so with their 24th shot of the tournament.
- Yann Sommer is just the third goalkeeper to score an own goal at the World Cup, after Honduras' Noel Valladares against France in 2014 and Spain's Andoni Zubizarreta against Nigeria in 1998.
- 2LichtsteinerBooked at 37mins
- 22SchärBooked at 83mins
- 11BehramiSubstituted forZakariaat 60'minutesBooked at 75mins
- 23ShaqiriSubstituted forLangat 81'minutes
- 18GavranovicSubstituted forDrmicat 69'minutes
- 19WastonBooked at 89mins
- 16GamboaBooked at 11minsSubstituted forSmithat 90+3'minutes
- 20GuzmánSubstituted forAzofeifaat 90+1'minutes
- 9ColindresSubstituted forWallaceat 81'minutes
- 12CampbellBooked at 29mins
- Clément Turpin
- Shots on Target
Second Half ends, Switzerland 2, Costa Rica 2.
Own Goal by Yann Sommer, Switzerland. Switzerland 2, Costa Rica 2.
Penalty missed! Still Switzerland 2, Costa Rica 1. Bryan Ruiz (Costa Rica) hits the bar with a left footed shot.
Substitution, Costa Rica. Ian Smith replaces Cristian Gamboa.
Penalty conceded by Denis Zakaria (Switzerland) after a foul in the penalty area.
Penalty Costa Rica. Joel Campbell draws a foul in the penalty area.
Substitution, Costa Rica. Rándall Azofeifa replaces David Guzmán.
Kendall Waston (Costa Rica) is shown the yellow card.
Offside, Costa Rica. Kendall Waston tries a through ball, but Bryan Ruiz is caught offside.
Goal! Switzerland 2, Costa Rica 1. Josip Drmic (Switzerland) right footed shot from the centre of the box to the bottom right corner. Assisted by Denis Zakaria with a cross.
Stephan Lichtsteiner (Switzerland) wins a free kick in the defensive half.
Foul by Rodney Wallace (Costa Rica).
Fabian Schär (Switzerland) is shown the yellow card for a bad foul.
Foul by Fabian Schär (Switzerland).
Joel Campbell (Costa Rica) wins a free kick in the attacking half.
Foul by Ricardo Rodríguez (Switzerland).
Johnny Acosta (Costa Rica) wins a free kick in the defensive half.
Substitution, Costa Rica. Rodney Wallace replaces Daniel Colindres.
Substitution, Switzerland. Michael Lang replaces Xherdan Shaqiri.