Reigning champions Brazil will face Spain in the Olympic men's football final after Marco Asensio's superb extra-time winner on Tuesday secured a 1-0 victory over host nation Japan in Saitama.
The Real Madrid winger came off the bench as a substitute and curled in a terrific strike in the 115th minute, sending Spain through to a first Olympic final since they finished runners-up at the 2000 Games in Sydney.
"We knew it was going to be difficult against a good team that was having a great tournament. We knew what we had to do and with patience, effort and hard work the reward had to come."
Japan will take on Mexico in Friday's bronze medal match. Mexico lost 4-1 on penalties to Brazil in Kashima following 120 goalless minutes.
Having needed a last-gasp equaliser against Ivory Coast in the previous round before prevailing 5-2 after extra time, Spain created the better chances over the 90 minutes against Japan without making the breakthrough.
Japan goalkeeper Kosei Tani produced a key save to deny Rafa Mir in the first half, while VAR intervened to overturn a penalty awarded to Spain after a tackle by Maya Yoshida on Mikel Merino.
Takefusa Kubo had a shot batted away by Unai Simon before Japan withstood a late siege, as Kou Itakura threw himself in the way of Martin Zubimendi's strike before Mir fired against the legs of Tani.
As a shootout beckoned Asensio conjured up a touch of magic, spinning free inside the area and bending his effort beyond Tani's despairing dive.
Earlier, Brazil goalkeeper Santos saved Eduardo Aguirre's penalty in a shootout before Johan Vasquez smacked the post with Mexico's second attempt.
Dani Alves, Gabriel Martinelli, Bruno Guimaraes and Reinier all scored for Brazil, who will be appearing in a fifth Olympic final on Saturday in Yokohama.
"It's very special to be able to take part in an Olympic Games and to get to the final. It's a really happy moment for me," said veteran Brazil captain Alves, who at 38 is one of three over-age players in the squad.
"Since the very start we set ourselves the aim of seeking gold, and I think we are deservedly in the final."
Mexico, who beat Brazil to win gold at London 2012, had scored 14 goals in four matches in this year's tournament – including six in their quarterfinal victory over South Korea.
Their best period against a disciplined Brazil came at the end of the first half as Luis Romo forced an excellent save from Santos, with Uriel Antuna's goalbound effort then blocked by Diego Carlos.
Brazil saw a penalty decision overturned by VAR after Jose Esquivel appeared to foul Douglas Luiz in the box.
Everton forward Richarlison, the competition's top scorer with five goals, nearly won the game for Brazil just before the end of normal time but his glancing header bounced back off the post.
Brazil won the 2016 final against Germany on penalties and are attempting to join Britain, Uruguay, Hungary and Argentina by defending their Olympic crown. Spain won the 1992 Games on home soil in Barcelona.