Match ends, Hungary 3, Portugal 3.
Portugal will play Croatia in the last 16 at Euro 2016 after Cristiano Ronaldo scored twice in a six-goal thriller against Hungary in Lyon.
A Zoltan Gera half-volley put Hungary in front but Ronaldo set up Nani to fire in an equaliser before the break.
Two deflected Balazs Dzsudzsak strikes twice restored Hungary's lead early in the second half, but a Ronaldo flick and header levelled on each occasion.
Hungary progress as Group F winners, with Portugal through in third place.
Portugal had been on course to play England in the last 16 as they occupied second place in the group at the final whistle in Lyon, but it was Iceland who claimed the runner-up spot with a dramatic injury-time winner against Austria in Paris.
Portugal, who would have crashed out had they lost to Hungary, have now reached the knockout stage in all seven of their European Championship appearances.
Hungary, who were denied a winner when Adam Szalai's shot hit the post, will now play Belgium, who finished second in Group E.
Relief for Ronaldo
Ronaldo appeared to be feeling the strain of not having scored at the tournament when, during a team stroll ahead of the Hungary match, he grabbed the microphone of a reporter and threw it into a river.
The 31-year-old came into the match with no goals from 20 attempts in the first two matches - including a missed penalty against Austria - and cut a forlorn and frustrated figure after Hungary took the lead through Gera's superb strike.
But the Real Madrid star set up Portugal's equaliser when he played a piercing ball for Nani to drive in a low shot which beat Gabor Kiraly at his near post.
And when Portugal twice fell behind to Dzsudzsak after the break, Ronaldo responded by rediscovering his scoring prowess just when it was needed.
His first goal - a deft flick from Joao Mario's cross - made him the first player to score in four different European Championship finals tournaments.
"A crazy game, we were out of the European Championship three times, and we were always chasing the game," said Ronaldo.
"It's not every team that manages to beat Spain, so we respect them [Croatia]. But we know our strength, we know our worth, and we'll look them in the eyes."
Surprise package Hungary
Unfancied Hungary have emerged as one of the feel-good stories of the Euros and have advanced from the group stage of a major tournament for the first time since the 1966 World Cup.
They conceded 63.2% of possession to Portugal but they scored three goals and caused Portugal's defence plenty of problems, even though manager Bernd Storck did not play four players on yellow cards.
They went ahead when 37-year-old former West Brom and Fulham midfielder Gera chested down a half-cleared corner before drilling home from 25 yards, while a free-kick and left-footed strike from Dzsudzsak went in off Andre Gomes and Nani respectively.
Akos Elek and Gergo Lovrencsics also went close, while a shot from Szalai effort struck the post in a pulsating game.
Man of the match - Cristiano Ronaldo
What they said
Hungary manager Bernd Storck: "I think it's a dream for the whole of Hungary - I'm speechless. The fans really supported us in the two previous games, but today it was even better."
Hungary midfielder Zoltan Gera: "We're on a fantastic journey. It means a lot to the team. The best wine is the oldest one, and the oldest wine is getting better and better, and I'm happy about that."
Portugal coach Fernando Santos: "This is what Cristiano is all about. He has to score because he's a winner. He plays up front and feeds on goals. This game increased his confidence levels."
The stats you need to know
- Only Michel Platini (9) has scored more goals at European Championships than Cristiano Ronaldo (8).
- Zoltan Gera is the second-oldest goalscorer at a European Championship (37 years, 61 days), after Austrian Ivica Vastic (38 years, 256 days)
- Gera's goal was his first for Hungary since since 14 November 2014 v Finland (586 days ago).
- Dzsudzsak is the first player to score two goals from outside the box in the same game at the Euros since Wayne Rooney (England v Croatia in 2004).
Portugal will play Croatia on Saturday, 25 June at 20:00 BST in Lens, while Hungary will play their last-16 tie on Sunday, 26 June at 20:00 BST in Toulouse.
- 23JuhászBooked at 28mins
- 20GuzmicsBooked at 13mins
- 10GeraBooked at 34minsSubstituted forBeseat 45'minutes
- 14LovrencsicsSubstituted forStieberat 83'minutes
- 7DzsudzsákBooked at 56mins
- 9SzalaiSubstituted forNémethat 71'minutes
- 1Rui Patrício
- 11Vieira de Freitas
- 10João Mário
- 14William Carvalho
- 8João MoutinhoSubstituted forRenato Sanchesat 45'minutes
- 15André GomesSubstituted forRicardo Quaresmaat 61'minutes
- 7Cristiano Ronaldo
- 17Almeida da CunhaSubstituted forDaniloat 81'minutes
- 2Bruno Alves
- 4José Fonte
- 5Jose Guerreiro
- 9Macedo Lopes
- 16Renato Sanches
- 18Ferreira Silva
- 20Ricardo Quaresma
- 21Cédric Soares
- 22Dos Reis Carvalho
- 23Adrien Silva
- Martin Atkinson
- Shots on Target
Second Half ends, Hungary 3, Portugal 3.
Foul by Renato Sanches (Portugal).
Adám Lang (Hungary) wins a free kick on the right wing.
Foul by Danilo Pereira (Portugal).
Ákos Elek (Hungary) wins a free kick in the attacking half.
Attempt missed. Cristiano Ronaldo (Portugal) right footed shot from outside the box is high and wide to the right from a direct free kick.
Cristiano Ronaldo (Portugal) wins a free kick in the attacking half.
Foul by Roland Juhász (Hungary).
Ricardo Carvalho (Portugal) wins a free kick in the defensive half.
Foul by Zoltán Stieber (Hungary).
Attempt missed. Cristiano Ronaldo (Portugal) left footed shot from more than 35 yards is close, but misses to the right. Assisted by Danilo Pereira.
Substitution, Hungary. Zoltán Stieber replaces Gergö Lovrencsics.
Substitution, Portugal. Danilo Pereira replaces Nani.
Ricardo Quaresma (Portugal) wins a free kick in the attacking half.
Foul by Mihaly Korhut (Hungary).
Offside, Portugal. Renato Sanches tries a through ball, but Cristiano Ronaldo is caught offside.
Attempt missed. Ricardo Quaresma (Portugal) right footed shot from outside the box misses to the left. Assisted by Renato Sanches.
Attempt missed. Cristiano Ronaldo (Portugal) right footed shot from the centre of the box is just a bit too high.
Offside, Hungary. Krisztian Németh tries a through ball, but Barnabas Bese is caught offside.