Mexico was on the verge of failing to qualify for next year's World Cup after losing 2-1 to Costa Rica, but El Tri stayed alive when the United States stunned Panama with two goals in the final minutes.

Costa Rica, which had already booked a spot in Brazil and was superior to Mexico throughout Tuesday night's match at Estadio Nacional in San Jose, opened the scoring in the 25th minute when Bryan Ruiz controlled the ball with his head in the area before finishing with a powerful left-footed blast.

Mexico tied it up four minutes later when Costa Rican goalkeeper Keylor Navas broke up a one-on-one scoring opportunity but batted the ball straight to Oribe Peralta, who fired a shot that deflected off the crossbar and into the net.

Costa Rica's superiority, however, was rewarded early in the second half when defenseman Cristian Gamboa lofted a perfect cross to Alvaro Saborio, who headed it home from close range.

The result left the Mexicans with just 11 points after the last match of the final six-team stage of CONCACAF World Cup qualifying, but they managed to finish fourth in the Hex and advance to an inter-confederation playoff next month against New Zealand thanks to a major assist from their most bitter rival, the United States.

The Americans, who finished first in the Hex, trailed Panama 2-1 Tuesday night at Estadio Rommel Fernandez in Panama City, but they pulled off a stunning 3-2 win thanks to a pair of stoppage-time goals by Graham Zusi and Aron Johannsson.

Had the Panamanians won, they would have had 11 points as well but beaten out Mexico and advanced to the playoff game based on goals scored.

Besides the United States, Costa Rica and Honduras also qualified for the World Cup directly out of CONCACAF.

Mexico has suffered through a miserable World Cup qualifying cycle that cost former coach Jose Manuel de la Torre his job last month. His replacement, Luis Fernando Tena, was then quickly replaced after a 2-0 loss to the United States.

Under current coach Victor Manuel Vucetich, El Tri notched a crucial 2-1 victory last week in Mexico City over Panama before losing to Costa Rica on Tuesday.

Mexico's national squad will next square off in a two-match World Cup playoff against New Zealand, with the first leg to be played in mid-November at Estadio Azteca and the second a week later in Wellington. EFE