The Japanese men looked set to join their women's team in setting up a gold medal match when a superb third goal of the tournament from Yuki Otsu gave them an early lead in this afternoon's semi-final.
But Marco Fabian, Oribe Peralta and substitute Javier Cortes turned the game around as Mexico set up a final on Saturday against Brazil or Republic of Korea and left their opponents playing for bronze.
Neither side had previously competed for gold and each had made the Olympic semis for only the second time, having last done so in 1968 when Japan went on to beat host nation Mexico in the bronze-medal match.
Despite some nervy defending early on, the Japanese were on course to emulate that result after 12 minutes today thanks to a stunning strike from Otsu.
Mexico goalkeeper Jose Corona, who had already tipped a Hiroshi Kiyotake shot wide, was helpless as Otsu's instant control from Keigo Higashi's pass set up a half-volley that the forward crashed into the top corner.
It was the first time Mexico had trailed at the Olympics and they needed to become the first side to score against Japan.
The pressure told in the 31st minute when Giovani dos Santos' corner was flicked on by Jorge Enriquez and Fabian nodded home from close range.
Neither side could force a second before half-time. But Mexico continued to press and completed their comeback in the 65th minute with a goal that was both sublime and ridiculous.
Peralta had just drilled too close to Shuichi Gonda, who rolled the ball out to Takahiro Ohgihara.
But the midfielder took too long and was robbed of possession by Peralta, the striker still having plenty to do as he drove an unstoppable strike right into the top corner.
Japan threw on three substitutes but they were unable to find a way through and were overcommitted in the final minute of stoppage-time as Cortes held off several weak challenges before firing the ball under Gonda.