A score draw was enough for Switzerland to secure their place in the final 16 of the 2018 World Cup.
Blerim Džemaili's thumping effort gave the Swiss the lead going into the break after yet another poor first-half performance.
Kendall Waston gave the Costa Rica fans their first goal to celebrate in the tournament after the turn of the half but the late drama stole the show.
Switzerland seemed to have won the game with a late strike from
Josip Drmić but a penalty from Bryan Ruiz was enough to snatch a late point for the Ticos.
Switzerland will now progress to the last 16 where Sweden await.
One step closer for Switzerland
Just after the 30-minute mark, Blerim Džemaili cannoned home the opening goal for Switzerland.
Stephan Lichtsteiner's cross was met by Breel Embolo who cleverly headered down to Džemaili instead of heading goalbound and the Bologna midfielder cannoned the ball beyond Keylor Navas.
The goal put the Swiss one step closer to securing their place in the final 16.
Ticos get their goal
Finally, the Costa Rica fans got their goal to celebrate and it was Kendall Waston who was the man who levelled the proceedings.
Joel Campbell's corner was latched onto by the defender whose header was placed perfectly in the corner - beyond the reach of Yann Sommer.
After two goalless games in Group E, Watson gave the thousands of travelling fans something to cheer about.
Josip Drmić gave Switzerland the lead in the 87th-minute with a fine strike inside the box.
Drimić latched onto the end of a cross from Denis Zakaria before coolly stroking the ball beyond Navas.
However, the celebrations were quickly ended when Costa Rica were awarded a penalty or so they thought.
After what seemed to be a foul on Bryan Ruiz the referee awarded Costa Rica a penalty before VAR overruling the decision as Ruiz was a yard offside before the foul.
Minutes later, the Ticos were awarded another penalty - this time the spot kick stood and Ruiz just about converted the penalty.
The shot rebounded off the crossbar before hitting the head of Yann Sommer and deflecting back into his own net.
Swiss prevail after challenging half
Costa Rica almost opened the scoring within the first six minutes when Celso Borges powered a header goalbound but a simply stunning save from Yann Sommer denied them their first goal of the competition.
Borussia Mönchengladbach's goalkeeper was quickly forced into another fine save - coming to the rescue of a Swiss side which looked rocked.
The Ticos continued to pressure the Swiss backline as Daniel Colindres struck from distance but his shot was denied by the crossbar and cannoning of the goal line.
After an opening half hour dominated by Costa Rica, it was Switzerland who opened the scoring.
Óscar Ramírez's side continued to press for their first goal of the 2018 World Cup but they were left ruing their missed opportunities when the referee blew for half-time.
It was another below-par performance from Switzerland in the first half, which was the case against both Brazil and Serbia.
However, Vladimir Petković's side for the first time in this tournament took the lead into the interval.
Action packed second-half
Straight from the referee's whistle, Joel Campbell had a golden opportunity on goal but the Arsenal midfielder's swing at the ball was not enough to turn the ball goal-bound.
Ricardo Rodríguez almost doubled Switzerland's lead with a thunderous volley which sailed a yard over Navas' crossbar.
The Ticos finally got their first goal of the 2018 World Cup just before the hour mark, making them the last team to score at the tournament and it took them 24 attempts to convert.
A well-deserved goal for a team who have been unfortunate not to have picked up a point in the tournament so far.
Following the Costa Rica equaliser, the tempo of the game fell with Switzerland looking settled on the idea of a draw.
However, out of nowhere Embolo broke down the right side and his cross was met by Josip Drmić whose header cannoned back off the woodwork - inches away from Switzerland edging the proceedings.
The game sprung back into life with just minutes remaining as Switzerland's late goal was cancelled out by Ruiz's penalty