Cristiano Ronaldo scored two penalties against France on Wednesday to equal the international goal-scoring record and help title holders Portugal edge into the last 16 of Euro 2020.
Ronaldo and his teammates registered a 2-2 draw against France in Budapest on Wednesday.
Portugal will face the world's top-ranked side Belgium in the first knockout round in Seville on Sunday.
Fernando Santos's men moved through as one of the four best third-placed sides for the second straight Euro tournament.
In Group F, they were behind France and Germany, who only just qualified after a dramatic 2-2 draw with Hungary in Munich.
Ronaldo gave his side the lead just after 30 minutes with his first spot kick, before France hit back through Karim Benzema's two goals either side of half-time.
Ronaldo, 36, scored his second penalty with 30 minutes remaining.
His fifth goal of the group stage extended his tally of Euro goals to 14 and drew him level with former Iran striker Ali Daei, in his 178th international match.
World champions France will next take on Switzerland in Bucharest on Monday.
The first real chance of the game fell to France forward Kylian Mbappe after quarter of an hour, but his wait for a Euro goal was extended by Rui Patricio's parried save.
It was Portugal who made the breakthrough, as they were awarded a 31st-minute penalty as France goalkeeper Hugo Lloris tried to punch the ball away.
Instead, he caught Danilo Pereira in the face, leaving the Portugal midfielder needing treatment on the pitch.
Ronaldo stepped up to score his fourth goal of the tournament, sending Lloris the wrong way.
France drew themselves level in controversial circumstances in first-half stoppage time.
Referee Antonio Mateu Lahoz gave a penalty after Mbappe collided with Nelson Semedo while trying to get on the end of a pass into the box.
After a long VAR check, the decision stood despite vociferous protests from the Portugal players.
Benzema, who missed his last three penalties, made no mistake from the spot this time, scoring his first goal for France since October 2015.
He was recalled for the tournament after a five-and-a-half year exile over his alleged role in a blackmail scandal.
It was also the Real Madrid striker's first goal at a European Championship in his ninth match.
Unsurprisingly, Danilo was substituted at half-time after the blow to his head, with Sporting Lisbon's Joao Palhinha taking his place.
France moved ahead for the first time shortly after the restart, though, as Paul Pogba's excellent pass released Benzema to slot in off the post.
The linesman flagged the Frenchman offside but a VAR check overturned the decision.
France were left without a recognised left-back when Lucas Digne hobbled off six minutes after coming on at the interval for Lucas Hernandez, who was booked in the first half.
But it was right-back Jules Kounde who conceded the second spot kick for a handball from Ronaldo's cross, and the Juventus star stepped up to level the all-time record, with Lloris guessing incorrectly again.
Portugal knew a defeat would send them out if Hungary beat Germany, and they needed Patricio to make a wonderful double save midway through the second half.
Pogba cleverly created space for a long-range strike that Patricio managed to claw on to the angle of post and crossbar, with the Wolves keeper getting up quickly to dive and push away Antoine Griezmann's follow-up effort.
Both teams pressed in search of a winner, with Griezmann firing over.
France had a penalty shout turned down in injury time despite another long VAR consultation.
Kingsley Coman went down under a challenge by Bruno Fernandes, who was brought on with 18 minutes to play.
But with the other match over in Germany, France knew a draw would put them through as group winners and that was how it finished.