The midfielder converted a penalty after Pepe had snatched an equaliser in stoppage time to cancel out an own goal from Luis Neto.
Andre Silva had missed from the spot in the first half before Mexico claimed their fortuitous breakthrough and Juan Carlos Osorio's side looked set to end their campaign in Russia in winning fashion before their late collapse.
Fernando Santos' men came into the match off the back of a shoot-out defeat to Chile in the semi-finals, in which all three of their spot-kick takers saw their efforts saved by Claudio Bravo, and there was to be a similar penalty problems at Spartak Stadium.
Veteran Rafael Marquez was punished for a poor challenge by a VAR referral but Andre Silva, who was handed the responsibility with Cristiano Ronaldo having been allowed to travel home to be with his new twins, saw his effort brilliantly saved by Guillermo Ochoa.
Portugal were the more attacking of the two teams for much of the opening hour but they were dealt a major blow when Neto inadvertently deflected Javier Hernandez's cross into the net.
Santos' men applied the pressure and found an equaliser in second-half stoppage time, Pepe poking home his first competitive international goal since June 2012, before Adrien Silva kept his cool to seal the win with both sides finishing a man light due to red cards for Nelson Semedo and Raul Jimenez.
Portugal spurned the chance to cap their strong start with the opening goal when Ochoa saved well from Andre Silva's spot-kick.
The AC Milan striker was scythed down by Marquez six yards from goal, but although referee Fahad Al Mirdasi awarded the penalty after a VAR consultation, Ochoa dived low to his right to keep out Silva's effort.
Nani headed over when unmarked in front of goal from the resulting corner as Portugal tried to keep up the early pressure, but it took a fine one-handed save from Rui Patricio to stop Mexico snatching the lead through Hernandez.
Gelson Martins had been one of Santos' more lively attackers and he almost broke the deadlock in fine style just after the break, latching onto a clever chip from Andre Silva before volleying just wide of the left-hand post.
The European champions had looked unsteady at the back, though, and a dreadful mix-up saw them fall behind through Neto's own goal.
Hernandez drove to the byline to the left of the area and drilled the ball back across goal and, when Patricio failed to intercept, the ball cannoned off Neto's shin and into the net.
Portugal went straight in search of an equaliser and missed three clear chances in quick succession. Andre Silva rolled a shot wide from just inside the area before Ochoa produced a brilliant one-handed save to keep out a Martins' header, and Nani nodded wide when he met the resulting corner completely unchallenged six yards from goal.
Hernandez was denied by a good Patricio block on the break as Portugal committed more men to the attack and their pressure told in injury time, when Pepe stretched a leg to prod in Ricardo Quaresma's cross from the right.
And Portugal made no mistake with their second penalty of the match 15 minutes into extra-time, Adrien Silva converting confidently after Miguel Layun had blocked Martins' flick with a raised arm.
Semedo and Jimenez both picked up second yellow cards for high boots in the closing stages but Portugal held firm for the bronze-medal position in their first appearance at this tournament.