Hungary and Portugal reached the Euro 2016 knockout stages after a rip-roaring 3-3 draw in which Cristiano Ronaldo netted twice and became the first player to score in four European Championships on Wednesday.
Portugal came from behind three times as Ronaldo, who also set a tournament record of 17 appearances, finally rediscovered his touch to help them secure a third straight draw that left them third in Group F and set up a last-16 meeting with Croatia.
Hungary, appearing in a major tournament for the first time in 30 years, topped the group with seven points thanks in large part to their captain Balazs Dzsudzsak, who scored two deflected goals.
Iceland also qualified in second after a last-gasp goal in their 2-1 win over Austria dramatically changed the outcome of the group. Until then, Portugal had been heading for a last-16 tie against England.
Hungary will now play the Group E runners-up, which will be decided later on Wednesday between Belgium, Sweden and Ireland, while Iceland will face England.
After Zoltan Gera's early strike was cancelled out by Nani just before the break, Dzsudzsak had twice put his side ahead soon after the break only for Ronaldo to respond each time with touches of his true mastery.
"The most important thing was to make it through to the next stage but we wanted to do it as the group winners," said Portugal coach Fernando Santos.
"That's why we pressed on for a fourth goal although we knew we were taking risks. Three times we were out of the tournament and three times we came back; it shows character akin to only a few teams."
While Portugal regularly reach the knockout stages of big tournaments, this is Hungary's first since they made the last four of the European Championship in 1972 and the first time they have got through a group stage since the 1966 World Cup.
"I am not surprised by the performance or the result," said Hungary coach Bernd Storck.
"We had a very good history in the 1950s and 60s but this is a bygone era and I want to talk about this team.
"We know Ronaldo is a world-class player and he cannot be stopped every time. But I am very happy how we played, we improved with each game in this tournament and are delighted to be in the last 16."
As in their previous two matches against Iceland and Austria, Portugal enjoyed sterile possession in the early stages and it was Hungary who struck in the 19th minute as 37-year old Gera took a half-cleared corner into his stride and unleashed a fizzing low-half volley past Rui Patricio from 25 metres.
Ronaldo then released Nani with a defence-splitting pass and his strike partner beat Gabor Kiraly with a neat finish three minutes before the break.
The Hungarians were back in front in the 47th minute as Dzsudzsak beat Patricio with a free kick that took a big deflection.
After throwing a reporter's microphone into a lake earlier in the day during a morning walkabout, Ronaldo, who had been struggling at Euro 2016, finally produced a touch of his finest work in the 50th, beating Kiraly with a clever back-heel after good work by Nani.
Hungary took the lead for the third time as Dzsudzsak seized a rebound in his stride on the edge of the area and fired a speculative shot which sailed past Patricio, again taking a deflection along the way.
Hungary's joy was short-lived, however, as Ronaldo levelled with a thumping header after a superb cross by substitute Ricardo Quaresma.
With the score ensuring both teams would progress, both then seemed happy to keep it at 3-3, although Iceland's winner did not delight the Portugal manager.
"Croatia are one of the sharks and we've been trying to avoid them," said Santos. "But now they are standing in our way and we have to hunt them down."