Portugal and Spain sealed their places in the last 16 of the FIFA World Cup as video assistant referee (VAR) controversy contributed to a dramatic conclusion to Group B at the tournament in Russia.
Spain topped the group on goals scored ahead of the European champions after Iran converted a late penalty, awarded on the advice of VAR, in a 1-1 draw with Portugal at the Mordovia Arena in Saransk.
Karim Ansarifard's spot-kick three minutes into stoppage time earned Iran a point and condemned Portugal to second place.
Another goal from Iran would have seen the Asian side top of the group, sending Portugal home in the process, and it nearly came when Mehdi Taremi hit the side netting after the ball fell to him in the box.
VAR also played a crucial role in Spain's thrilling 2-2 draw with Morocco at the Kaliningrad Stadium as Iago Aspas' 91st minute equaliser was eventually given following a long delay while video officials checked for offside.
The two incidents occurred at almost identical moments in the respective matches and ensured a pulsating, yet controversial end, to Group B.
Spain, World Cup winners in 2010, had to come from behind as Youssef En-Nesyri looked to have won it for Morocco nine minutes from time.
Aspas flicked in Dani Carvajal's low cross to equalise but referee Ravshan Irmatov disallowed the goal for offside.
Spanish blushes were spared by the VAR, with Irmatov then altering his decision after checking video footage.
Spain's reward for finishing top of the group is a meeting with Russia in the knockout round after the host nation were beaten 3-0 by Uruguay in their final Group A encounter at the Samara Arena today.
Uruguay, semi-finalists in 2014, will take on Portugal in an eagerly-anticipated clash for a place in the last eight.
Portugal looked to be heading towards first place in the group when Ricardo Quaresma scored a stunning opener - and goal of the tournament contender - on the stroke of half-time.
Quaresma cut in from the right flank and played a one-two before curling a shot into the far corner of the net with the outside of his boot.
Cristiano Ronaldo, who scored four goals in Portugal's opening two matches, then missed a golden opportunity to double his side's lead as his penalty - awarded after referee Enrique Cáceres checked the VAR and reversed his decision - was saved by Iran goalkeeper Ali Beiranvand.
Chances were few and far between for Iran, who had to win the match to have a chance of causing an upset by reaching the last 16, until they were given a contentious penalty in the 93rd minute.
Sardar Azmoun's knock-down from a cross hit the outstretched arm of Portugal's Cedric Soares but referee Enrique Cáceres initially rejected calls for a penalty.
Despite the VAR proving inconclusive that a clear and obvious error had been made, Cáceres changed his decision and Ansarifard duly stepped up to set up a nervy finale for Portugal.
There was also drama in Spain's match with Morocco as the African nation broke the deadlock in the 14th minute through Khalid Boutaib.
Isco rounded off a well-worked move to equalise five minutes later but Spain remained disjointed and were unable to produce the form of previous Spanish teams.
Morocco sensed the chance to claim their first win and En-Nesyri moved them to the brink of earning all three points when he headed home substitute Faycal Fajr's corner.
But Aspas' leveller saw Spain progress as group winners and ensured Morocco returned home without a win.
Group A failed to provide similar drama as Uruguay and Russia had already guaranteed their place in the last 16.
The two teams met with only first place in the group left to be decided and Uruguay outclassed the hosts, who ended the match with 10-men after Igor Smolnikov was sent off for two bookable offences.
Luis Suarez found the bottom corner from a free-kick to give Uruguay the lead before Denis Cheryshev's own goal made it 2-0 to the South Americans.
Edinson Cavani completed a resounding win for Uruguay with a third in the 90th minute.
Saudi Arabia beat Egypt 2-1 at the Volgograd Arena in the Group A dead rubber as the African team's goalkeeper Essam El-Hadary became the oldest player to feature at a World Cup at the age of 45.
El-Hadary saved a penalty but was unable to prevent his side falling to their third straight defeat.