It was the perfect end to a bizarre day of World Cup qualifying: the United States saved Mexico.

The longtime rivals rarely do anything to help the other out, but on Tuesday there was no mistaking what took place. Mexico, which has struggled throughout the Concacaf qualifying tournament, was just minutes away from losing to Costa Rica, 2-1, and failing to qualify for the World Cup for the first time since 1982. El Tri’s only hope was that the United States, which was losing to Panama at the time, would somehow rally for at least a tie.

The United States did even better, scoring two late goals to beat Panama, 3-2. The result knocked the Panamanians out and gave Mexico an unexpected — and some might say undeserved — reprieve. Mexico, which has won just 2 of its 10 qualifiers and is on its third coach of the cycle, will now face New Zealand in a two-game playoff for a place at the World Cup next summer in Brazil. The United States clinched its place last month after beating Mexico in Columbus, Ohio.

The United States, without many of its top players, had little to play for but was game for most of the match and tied the score, 1-1, in the 63rd minute — a development that coincided almost exactly with Costa Rica taking the lead on Mexico in its game.

Undeterred, Panama bounced back to reclaim the lead in the 83rd minute and looked set to go through to the playoff, only to collapse in the final seconds, allowing Graham Zusi to score in the second minute of added time and Aron Johannsson to score a minute later to complete the turnaround for the United States and, for at least this night, its biggest rival, too.