Mexico advanced to the Confederations Cup semifinals as Group A runners-up by rallying for a 2-1 win over hosts Russia, who were eliminated.
Needing a victory to advance from their difficult group, Russia took the lead with a goal by Alexander Samedov in the first half, but the experienced Mexicans equalised through Nestor Araujo before half-time and secured the victory early in the second with a goal by Hirving Lozano after a blunder by Russia goalkeeper Igor Akinfeev.
The hosts wanted a penalty after Yuri Zhirkov fell inside the area in the sixth minute, but he was barely touched and the referee let the play continue. Zhirkov then nearly opened the scoring for Russia, but his low left-foot shot missed just wide.
Russia appealed for another penalty about 10 minutes later when striker Fedor Smolov went down in the box after getting tangled up with Araujo, but no penalty was given despite help from video. Play had continued for about 30 seconds before the game was stopped for the review.
Smolov then struck the post with a long-range shot and Zhirkov had his shot saved by Mexican goalkeeper Guillermo Ochoa.
But Russia kept possession and sent the ball back into the area, where Alexandr Erokhin missed his kick but still had time to lay the ball off for Aleksandr Samedov to net the opener in the 25th minute.
But Mexico got back level five minutes later as Nestor Araujo got on the end of Hector Herrera's cross into the box to put a looping header over Akinfeev.
Erokhin had a superb chance before half-time after entering the area free from markers in a breakaway in the 33rd minute, but he sent his shot over the crossbar.
Mexico completed their comeback shortly after half-time when Herrera launched a long ball forward from his own third. It took a high bounce just outside the box, and Hirving Lozano beat Akinfeev to the ball.
The goalkeeper apparently wasn't sure if he could use his hands and ended up taking too long to commit to the ball with his feet, allowing Lozano to nip in and head it into the open net.
Moments later, Hector Moreno thought he had a third goal for Mexico -- which would have seen them take the group lead over Portugal -- but it was ruled out for offside following a video review.
And in the 68th minute, Russia's hopes of an equaliser were dealt a blow when Zhirkov was sent off with a second yellow card after elbowing Miguel Layun.
But one of Russia's best chances was still to come when substitute Igo Smolnikov also sent his shot over the crossbar from very close range.
Mexico continued to push for a third goal in pursuit of topping the group, but two late goals in the group's other game gave Portugal a cushion to finish top on goal difference.
Mexico will next face the winners of Group B in Sochi on Thursday, when Andres Guardado will be suspended because of yellow card accumulation.
Russian players gathered in the middle of the pitch to salute fans after the final whistle and were applauded by the crowd of 41,585 fans at Kazan Arena..
Information from The Associated Press was used in this report.