Iran's late, late push to reach the last 16 of the World Cup for the first time came up just short despite Karim Ansarifard's injury-time equaliser from the spot.
There was drama, controversy but not quite the fairytale ending for Team Melli as Portugal hung on for a draw to claim second spot in Group B behind winners Spain.
Everything seemed to be lining up the European champions' way as the clock ticked over into injury time as they looked set to top the group and line up a second round clash with hosts Russia.
Cristiano Ronaldo's missed penalty early in the second half looked set to be irrelevant as they led 1-0 while, in Kaliningrad, Morocco had taken a shock lead against Spain through Youseff En-Nesyri.
In need of a win to progress, the underdogs pumped a hopeful ball into the box towards the imposing Sardar Azmoun, and his header brushed the hand of Cédric.
The Portuguese didn't appear to know much about it – and referee Enrique Caceres didn't see an infringement initially – but he was guilty of waving his hand.
Caceres was persuaded to consult VAR and, after a lengthy delay, the Paraguayan referee changed his decision to award a second penalty of the night.
Ansarifar's expertly-dispatched penalty, together with Iago Aspas' near-simultaneous equaliser for Spain, changed the complexion of the group with Spain now top on goal difference.
It could still have gotten worse for Portugal as Iran, with all the momentum and needing a goal to take their place in the second round, had one final chance.
Another hopeful ball pumped forward found replacement Saman Ghoddos, and his effort was deflected into the path of Mehdi Teremi who, with glory beckoning, hit the side netting.
With it went Iran's chance of making history, but they can at least take solace from having been part of one of the tournament's most thrilling games.
Carlos Quieroz's charges set up in a similar manner to their narrow loss to the Spanish last week, sitting back and allowing their opponents to hold onto the ball.
Ronaldo stung Ali Beiranvand's fingers with an early strike, but the goalkeeper was to prove considerably less stable under the high ball.
Miscommunication between the keeper and defender Seaid Ezataholi led to an early chance for Joao Mario, and he was fortunate not to concede after woefully misjudging two simple crosses.
Iran looked dangerous on the counter and they had chances, first when Rui Patricio bravely snatched the ball off Alireza Jahanbakhsh's toe, before Ezataholi headed straight at the keeper.
And they looked to have made it to half time unscathed when Ricardo Quaresma produced a piece of barely comprehensible magic, curling into the top corner with the outside of his right boot.
It was a goal of splendour not entirely befitting what was, until then, a scrappy and somewhat slow-paced game, but it picked up considerably after the restart.
The second half was just five minutes old when Ronaldo fell under the clumsy challenge of Ezataholi and, after referring to VAR, the referee eventually pointed to the spot.
The Real Madrid striker, who had netted from the spot in the opening game against Spain, couldn't replicate the feat as he put his left his kick too close to Beiranvand.
The perceived injustice lit the wrong sort of fire in Iran, who had Ehsan Hajsafi and Azmoun booked in quick succession, but it was to be Portugal who lost their cool as the game wore on.
Quaresma was booked for a petulant tackle and quickly hooked, while Ronaldo was extremely lucky only to see yellow for striking Morteza Pouraliganji.
Referee Caceres missed the incident in real time but, once again, consulted VAR before issuing a booking to Ronaldo, who was visibly fearing the worst.
Ansarifard's penalty had the entire Portuguese side fearing the worst, but they hung on through eight minutes of added time to book a Saturday night date with Group A winners Uruguay.
Iran: Alireza Beiranvand; Ramin Rezaeian, Majid Hosseini, Morteza Pouraliganji, Ehsan Hajsafi; Mehdi Taremi, Alireza Jahanbakhsh (Saman Ghoddos 70), Saied Ezatolahi (Karim Ansarifard 75), Omid Ebrahimi, Vahid Amiri; Sardar Azmoun.
Subs not used: Mohammad Mazaheri (gk), Amir Abedzadeh (gk), Mehdi Torabi, Milad Mohammadi, Masoud Shojaei, Mohammed Reza Khanzadeh, Pejman Montazeri, Reza Ghoochannejhad, Ashkan Dejagah.
Booked: Ehsan Hajsafi (52), Sardar Azmoun (54),
Portugal: Rui Patricio; Cédric, Pepe, José Fonte, Raphael Guerreiro; Ricardo Quaresma (Bernardo Silva 69), William Carvalho, Adrien Silva, Joao Mario (Joao Moutinho 84); Andre Silva (Goncalo Guedes 90+6), Ronaldo.
Subs not used: Beto (gk), Anthony Lopes, Bruno Alves, Manuel Fernandes, Rúben Dias, Ricardo Pereira, Bruno Fernandes, Gelson Martins, Mario Rui.
Booked: Raphael Guerrero (33), Ricardo Quaresma (64), Cristiano Ronaldo (83), Cédric (90+8).
Referee: Enrique Caceres (Paraguay).
Attendance: 41,685 (official).
Extratime.ie Player of the Match: Sardar Azmoun (Iran).