A controversial injury-time penalty given after a VAR review salvaged a draw for Iran after Ricardo Quaresma's fine goal had given Portugal the lead and Cristiano Ronaldo had seen a spot-kick saved.
The late drama came when Sardar Azmoun headed the ball against Cedric Soares at close range, with referee Enrique Caceres giving the penalty after reviewing video footage.
Moments later, Iran had a chance to grab the goal that would have put them through at Portugal's expense, but Mehdi Taremi hit the side-netting and Portugal finished second in Group B as Iran went out of the tournament.
It took Ronaldo three minutes to have his first attempt of the game, cutting in but firing his attempt straight at Iran goalkeeper Alireza Beiranvand.
But Iran created an opening only a few minutes later, Alireza Jahanbakhsh doing well down the right and sliding a low pass across the edge of the box, where Vahid Amiri could only scuff well wide.
Beiranvand and his defenders got into a muddle as the game's open start continued, the ball rebounding to the edge of the area where Joao Mario sent a hurried finish over what was almost an open goal.
The Iran keeper was having an uncertain start and, when he came off his line but failed to claim another cross, the ball was cleared before Andre Silva could take advantage.
After 16 minutes, Ronaldo tried his luck from a long-range free kick but the ball cannoned off the wall and over the bar with the Portugal captain appealing in vain that it had struck an arm.
Adrien Silva gave away a free kick 25 yards out on the left of the Portugal area, but Ramin Rezaeian's strike was off target.
Iran responded, Rui Patricio bravely out to claim a ball into the area just ahead of the onrushing Jahanbakhsh, before Saeid Ezatolahi headed straight at the keeper.
Five minutes from the break, Ronaldo dropped deeper and hit a long-range shot that took a slight deflection and was comfortably fielded by Beiranvand.
But in the last minute of a half that had been anything but easy, Portugal broke through in style as Quaresma made his way to the edge of the area and curled a stunning effort into the corner with the outside of his boot.
After a lacklustre start to the second half, the game burst into life when Ronaldo took possession on the edge of the area and went down after a tangle with Ezatolahi.
The referee waved play on but a VAR review gave a penalty -- only for Ronaldo's kick to be parried and then held by Beiranvand, denying the Portugal captain the chance to move level with Harry Kane on five tournament goals.
Iran had Haji Safi and Azmoun booked for dissent as things threatened to boil over, with Safi substituted for Milad Mohammadi shortly afterwards.
Azmoun went down inside the area after colliding with Cedric but the referee waved away his appeals before Iran threatened again as a dangerous cross from the right was cut out.
Just past the hour, Quaresma was booked for a crunching challenge on Ezatolahi before Ronaldo found a pocket of space but lashed a fierce strike wide.
Quaresma made way for Bernardo Silva as the game entered its final 20 minutes, while Iran brought on Saman Ghoddos for Jahanbakhsh, Ghoddos swiftly into the action as he span on the edge of the area but lashed wide.
Azmoun went down under a challenge in the Portugal area, a VAR check upholding the referee's decision of no penalty, before Ezatolahi came off for Iran, Karim Ansarifard replacing him.
Another VAR review, this time for a challenge by Ronaldo on Morteza Pouraliganji, ended in a booking for the Portugal captain as an increasingly fractious game headed for its final five minutes with his side still ahead.
Joao Mario came off for Joao Moutinho and Iran appealed for another penalty when they claimed that the ball had hit Cedric's hand from a header down - and when the referee ruled it was a penalty Ansarifard stepped up to level from the spot.
Moments later Taremi's chance went wide, and Iran's last hopes went with it.