Portugal have fallen to a second-placed finish in Group B after a controversial penalty decision handed Iran an equaliser late on in a 1-1 draw in the Mordovia Arena, as Spain held onto first place with a 2-2 draw against Morocco.
The European champions, who will now face Uruguay in the Round of 16, struggled to break Carlos Queiroz's side down before Ricardo Quaresma's moment of magic on the brink of half-time promised to be the decider throughout the encounter.
A late penalty stole the show, however, converted by Karim Ansarifard, with the referee coming to a controversial conclusion when reviewing footage from VAR, as the two nations shared a point each.
Santos rigns the changes
Coming into the encounter, Fernando Santos brought changes to his XI, as Adrien Silva, Quaresma and André Silva earned their first starts of the tournament, meanwhile Iran called on the return of AZ Alkmaar's Alireza Jahanbakhsh for their must-win game.
Despite being typically organised, Queiroz's side did offer Portugal an early sighting of goal following a miscommunication at the back between the keeper and his defence, as João Mário flashed his volley over from the edge of the box in the 10th minute, letting Iran off the hook.
With panic drying up at Alireza Beiranvand's end, it wasn't until the 33rd minute that the Iranians let fly at Portugal's goal via Saeid Ezatolahi's header, meeting Rui Patrício's grasp within the goal.
Frustrations led Cristiano Ronaldo to try his luck from distance in the 40th minute, without success, reserving the great moment of inspiration for Quaresma on the brink of half-time, as the 34-year-old stunned the Mordovia Arena by placing Portugal ahead with the outside of his boot, arcing a shot from range into the top corner.
Into the second period, with Spain and Morocco locked at 1-1 in Kaliningrad, the Seleção began to count down the minutes separating them from a potential first-placed finish in Group B.
Emotions rose almost immediately after a clumsy challenge from Morteza Pouraliganji on Ronaldo handed Portugal a penalty, with the help of VAR. The Real Madrid star would scupper the chance to double the lead, however, as Beiranvand kept his side in it with a save in the 52nd minute.
Keen to make amends for the penalty miss, Ronaldo worked his way to the edge of the box in the 63rd minute before firing at goal, but again the Portugal captain would be left unhappy at the sight of his shot wide.
Second-half substitute Saman Ghoddos looked to keep Iran's hopes of qualification alive at the other end, digging a shot out from under his feet that would roam close to Patrício's near-post in the 73rd minute.
VAR red card review
A heart-in-mouth moment saw the referee analyse VAR footage over a potential red-card offense by Ronaldo, but a yellow card is all the number seven would receive going into the final ten minutes of the game, meanwhile Morocco made it 2-1 versus Spain.
More drama was reserved for stoppage time, just as Spain equalised in the other game, with Iran, rather controversially, being given the opportunity to do the same from the spot following Cedric Soares' alleged handball.
Karim Ansarifard converted his penalty and Iran came desperately close to turning the game on its head just minutes later through Mehdi Taremi, who'd fire a shot into the side-netting from inside the box.
With little time for a solution, Portugal were left to settle for second place behind Spain, matching Santos' men up against Uruguay in the next round, as Iran crashed out on four points.
By Patrick Ribeiro
Portugal: Rui Patrício - Cedric Soares, Pepe, José Fonte, Raphaël Guerreiro - Ricardo Quaresma (Bernardo Silva, 70'), William Carvalho, Adrien Silva, João Mário (João Moutinho, 82) - Cristiano Ronaldo, André Silva
Iran: Alireza Beiranvand - Ramin Rezaeian, Morteza Pouraliganji, Jalal Hosseini, Haji Safi (Milad Mohammadi, 57') - Saeid Ezatolahi (Karim Ansarifard, 76'), Omid Ebrahimi, Vahid Amiri - Mehdi Taremi, Alireza Jahanbakhsh (Saman Ghoddos, 70') - Sardar Azmoun
[0-1] - Ricardo Quaresma (44')
[1-1] - Karim Ansarifard (93')