Cristiano Ronaldo produced a stunning performance to keep Portugal in Euro 2016 with a 3-3 draw against Hungary in Lyon.
Ronaldo, who made a record 17th European Championship appearance in the match, set up Nani for Portugal's first and then scored twice in the second half to become the first player to score in four different European Championships.
Balazs Dzsudzsak, meanwhile, scored a brace of his own for Hungary, who led three times during the match and clinched top spot in Group F.
Portugal finish third and face Croatia in the round of 16 on Saturday, while Hungary take on the runners-up from Group E on Sunday.
Portugal, who faced a group-stage exit with defeat, made most of the early running without truly threatening. They had a first sight of goal with around five minutes on the clock when a free kick bounced out to Joao Mario on the edge of the area, but the midfielder sent his shot over the bar.
However, there had been signs that Hungary -- entering the match as the surprise leaders of Group F -- were seeking more than just the point that would guarantee a top-two finish, and on 19 minutes they snatched the lead.
The ball fell to Zoltan Gera 25 yards out and the 37-year-old former West Bromwich Albion and Fulham midfielder hammered an outstanding half-volley past goalkeeper Rui Patricio and low into the corner. In doing so, he became the second oldest goal scorer in European Championship history, behind Austria's Ivica Vastic.
Ronaldo brought a good save from Hungary keeper Gabor Kiraly -- the European Championship's oldest ever player -- when presented with a free kick from 35 yards on 29 minutes. Shortly afterwards, the Real Madrid man had another sight of goal from a free kick but sent it way off target.
They finally levelled on 42 minutes when Ronaldo slipped an inch-perfect pass through to Nani, who drilled home at the near post, with Kiraly unable to get down in time to make the stop.
The scores were tied going into the second half, but Hungary had restored their advantage within two minutes of the restart after winning a free kick just outside the area. Dzsudzsak stepped up and sent his shot past Patricio, with the aid of a substantial deflection from Andre Gomes.
Gergo Lovrencsics then threatened to increase their lead but could only find the side netting, and Ronaldo soon took advantage, producing a superb flick in the 50th minute from Joao Mario's cross to make it 2-2.
Hungary were soon ahead once more. Dzsudzsak got his second of the match when, after his free kick had been charged down, he regained possession and saw his shot take a huge deflection off Nani on its way in.
Once more, the lead did not last long. Ricardo Quaresma, who had just been introduced from the bench, sent in a cross for Ronaldo to head home to complete his brace.
Akos Elek then came close to giving Hungary the lead for a fourth time when his shot came back off the inside of the post, but Portugal survived.
That proved to be the last clear chance of the match as the teams shared the spoils and sealed their progress from Group F.