Japan 1-2 Mexico
Two second-half headers by Javier Hernandez saw Mexico depart the Confederations Cup on a winning note as Japan finished bottom of Group A.
Andres Guardado, who headed against the woodwork, provided the cross which saw Hernandez open the scoring after a fine run by the Manchester United man.
Both teams were already eliminated, but Mexico sealed the win when Hernandez scored from Giovani Dos Santos' corner.
Shinji Okazaki scored a consolation before Hernandez missed a late penalty.
Hernandez was hauled down by substitute Atsuto Uchida before the 25-year-old got to his feet to take the spot-kick which was kept out by Eiji Kawashima.
In a dramatic finale, the ball rebounded back to Hernandez who missed out on a hat-trick by blazing his second attempt against the bar.
However, the miss did not prove costly as Mexico finished third behind Brazil and Italy in the four-team group.
The only consolation for Japan, who return home with no points from three games, is that they know they will be returning to Brazil in 12 months' time after confirming their place at the 2014 World Cup finals as Asian Group B winners.
Whether Mexico will be back remains to be seen.
They still have plenty of work ahead to qualify for the World Cup, but coach Jose Manuel de la Torre will be buoyed by his team's performance against Japan after back-to-back defeats by Italy and Brazil.
There was no sign of what was to come as Japan created a number of early chances.
Manchester United's Shinji Kagawa should have scored his second goal of the tournament before Yasuhito Endo's long-range attempt found the Mexican net via a deflection off team-mate Okazaki.
But the goal was ruled out as Okazaki was offside. Okazaki, who plays his club football in Germany for Stuttgart, then went close after a determined run before Keisuke Honda forced a save from Guillermo Ochoa.
Japan's wasteful finishing proved their downfall.
Mexico went closest to scoring before the break when Guardado's flying 40th-minute header rebounded off the woodwork.
But they secured victory with two goals in 12 minutes after the interval through Hernandez, who now has 35 international goals in 53 appearances.
His first came after a well-timed run to meet Guardado's 54th-minute pin-point cross.
The second was from close range, the striker rising to meet Mier's flick on following a 66th-minute corner.
Japan set up a thrilling finish when Okazaki scored from close range with four minutes remaining after Endo's ball across the six-yard box.
But the drama was not over as Hernandez won and then missed a penalty in Estadio Mineirao.