Portugal claimed third place in the Confederations Cup with a fiery 2-1 extra-time victory over Mexico in Moscow.
The European champions were within seconds of losing in normal time after central defender Luis Neto had put through his own goal after 54 minutes.
But Pepe equalised after the clock had ticked past 90 minutes, before Adrien Silva converted from the penalty spot at the end of the first period of extra time.
Both sides were reduced to 10 men in the final stages as Portugal defender Nelson Semedo and Mexico substitute Raul Jimenez were dismissed for two bookable offences.
Portugal were without their talisman after Cristiano Ronaldo had been excused duty to see his new twins.
And Ronaldo’s presence was sorely missed as Portugal passed up a raft of chances, including missing yet another penalty following their spot-kick misery during the semi-final defeat to Chile.
Andre Silva was bundled over by Rafael Marquez’s rash 15th-minute challenge, but the spot kick was only awarded after the Video Assistant Referee (VAR) system showed that contact had been clearly made.
Portugal had failed to score in the shoot-out defeat to Chile and a fourth consecutive penalty was missed when Andre Silva’s kick was brilliantly turned aside by Guillermo Ochoa.
Mexico’s goal continued to lead a charmed life as stand-in skipper Nani, Pizzi, Semedo and Gelson Martins all went close to breaking the deadlock.
Rui Patricio produced a brilliant one-handed stop to prevent Javier Hernandez from giving Mexico the lead against the run of play after 31 minutes.
But Mexico did go ahead in the second half when Neto, making his first appearance in the competition, turned Hernandez’s cross into his own net from six yards out.
Carlos Vela should have doubled the lead and secured Mexico’s first-ever win over Portugal in five attempts.
It was a costly miss as Pepe swept home Ricardo Quaresma’s cross in the dying seconds and Adrien Silva’s 104th-minute penalty settled the contest after Miguel Layun had handled.