Cristiano Ronaldo broke his Euro 2016 duck as Portugal drew 3-3 with Group F winners Hungary to secure their passage through to the last 16.
Portugal came from behind three times in a breathless tie in Lyon and progress as one of the four best third-placed teams.
Hungary have been one of the surprise packages of the tournament, with a win over Austria and draw with Iceland guaranteeing their progress prior to this match.
Coach Bernd Storck made five changes to his side but they certainly did not rest on their laurels and took the lead in the 19th minute through one of the goals of the tournament from Zoltan Gera.
Nani levelled matters just before half-time and a crazy 17-minute spell after the break saw Hungary captain Balazs Dzsudzsak twice put them back into the lead only for Ronaldo to cancel it out both times.
After missing a penalty against Austria, the Real Madrid star found the net with his 24th effort of the tournament to become the first player to score at four different European Championship finals.
Portugal had been held to draws by both Iceland and Austria, and their hopes of reaching the last 16 were dealt a blow when Gera struck.
The 37-year-old Ferencvaros playmaker controlled the ball 25 yards out after a corner was half cleared by Nani before sending a superbly-struck half-volley arrowing into the bottom corner.
Hungary might have added a second five minutes later after a defensive mix-up allowed Akos Elek a clear sight of goal but goalkeeper Rui Patricio kept out his shot.
Ronaldo’s first sight of goal came from a free-kick but it was pushed behind by Gabor Kiraly, who was wearing his customary grey tracksuit bottoms despite the 30-degree temperatures.
Portugal were then correctly denied by the offside flag but two minutes later they did find their equaliser, Ronaldo turning provider with a perfect through ball for Nani to beat Kiraly at his near post.
Both sides made changes at the break – Renato Sanches coming on for Joao Moutinho for Portugal while Barnabas Bese replaced Gera.
It took Hungary, who had lost all five previous competitive encounters with Portugal, only two minutes after the break to regain their lead.
Dzsudzsak lined up a free-kick 25 yards out to the right of goal and his deflected strike beat Patricio.
Gergo Lovrencsics then shot into the side-netting as Hungary threatened again but their lead lasted only three minutes before Ronaldo struck.
The 31-year-old – who set another record by playing in his 17th match at European Championship finals, surpassing Lilian Thuram and Edwin van der Sar – produced a typically sublime piece of skill as he met Joao Mario’s cross with a back flick.
But five minutes later Hungary scored again, and it was another deflected strike from Dzsudzsak.
This time his free-kick from an almost identical spot was blocked by the wall but the ball came back to him and he was allowed time and space to cut inside and find the far corner.
Portugal boss Fernando Santos sent on Ricardo Quaresma, who had been left out of the starting line-up, and with virtually his first touch he provided a perfect cross for Ronaldo to head in yet another equaliser.
Portugal exerted most of the late pressure, although Elek hit a post, but they were unable to find a winner.