Mexico rallied from two goals down at half-time to defeat Trinidad and Tobago 4-2 and win Group B at the Pan Am Games.
Ricardo John opened the scoring for Trinidad just seven minutes in, and Schakeil Henry doubled the advantage in the 39th minute.
But the second half belonged to Mexico, Jorge Espericueta drawing one back on the hour mark befre substitute Daniel Alvarez equalised eight minutes later.
Marco Bueno found the winner in the 90th minute and Martin Zuniga capped the scoring in stoppage time.
Mexico will face Group A runners-up Panama in the semifinals on Thursday.
Uruguay defeated Paraguay 1-0 to join Mexico in the medal round.
Fernando Gorriaran scored the game's only goal in the 52nd minute and Uruguay, knowing only a win would be enough to jump Paraguay in the table, held on to advance as runners-up.
Brazil await Uruguay in the other semfinal on Thursday.