Cristiano Ronaldo delivered two goals and an assist to secure the 3-3 draw Portugal needed against already qualified Hungary to ensure progress to the Euro 2016 knock-out stage on Wednesday.
Zoltan Gera gave Hungary the lead after 19 minutes and captain Balazs Dzsudzsak found the net with two deflections early in the second half in Lyon.
But Portugal equalized each strike with man of the match Ronaldo setting up Nani before the break and then finding his first goals in the tournament after the interval. The first was a stunning flick from Joao Mario's cross and the second a powerful close-range header.
Hungary progress as Group F winners on five points and face the runners-up from Group E, likely Belgium, in the last 16 while Portugal, on three, go through in third-place to meet Croatia. Iceland beat bottom team Austria 2-1 in the other game to go through as runners-up behind Hungary on goal difference and meet England.
Portugal had dominated their previous matches but only scoring one goal in two games resulted in two draws and meant they could not afford to lose against Hungary.
"It's a privilege for me to be named as man of the match," Ronaldo said in response to a question asked by a UEFA media officer. He refused to take questions from reporters in the customary post-match news conference, saying he would move to the mixed zone - which he then passed through without stopping.
"But our priority was to win the match, we failed to do that. We played a good match, it was a little insane, Hungary attacked, we attacked, there were many goals."
Bernd Storck's Hungary were already guaranteed to progress and made five changes to the team which drew with Iceland last time. All the players who had previously been booked were left on the bench to avoid the risk of suspension.
"A big compliment to my team, we have improved our performance compared to the first two games," Storck said "We knew it would be a hard game.
"My general impression is it was an excellent game, (it was) excellent progress for our team."
Hungary pushed forward on the break when possible and snatched the lead slightly against the run of play. Nani's poor headed clearance from a corner fell perfectly for veteran Gera to control on his chest and lash a superb left-foot strike into the bottom corner.
The 37-year-old Gera became the second oldest player to score at a Euro finals, UEFA said.
Portugal levelled three minutes before the break as a pin-point pass from Ronaldo split the Hungary defence and Nani atoned for previous errors with a low finish past keeper Gabor Kiraly at the near post.
The match exploded into life after the restart as Dzsudzsak's deflected free kick wrong-footed keeper Rui Patricio but Hungary's lead lasted only 120 seconds.
There appeared little danger as Ronaldo went to meet a cross from the right that was behind him but he sent a magnificent backheel flick across Kiraly and into the far corner.
The strike made Ronaldo the first to score at four different European Championships finals.
"This is another record but I always say that comes naturally," Ronaldo said in a flash interview. "The main thing was to make it through."
Dzsudzsak restored the lead in 55, cutting inside from the right to fire home from the edge of the box with the aid of another deflection but once more Portugal - through Ronaldo - had the answer.
Substitute Ricardo Quaresma had only just been introduced after the hour but his cross from a short corner found Ronaldo on the six-yard line and his header left Kiraly helpless.
"I want to praise my players because three times we were out of the next stage," Portugal coach Fernando Santos said. "I have to praise my team because they managed to draw.
"But we knew the most important thing was to qualify for the next stage."
Play raged to the other end and Akos Elek almost gave Hungary the lead for the fourth time with a shot which smacked the inside of the post.
Portugal dominated the closing stages but could not find a late winner.
"We tried but we were still cautious," Santos said, adding his team could not push forward too much and risk elimination.
Saturday, June 18, 2016 - 20:59
Sunday, June 19, 2016 - 10:59
Monday, June 20, 2016 - 05:05