Cristiano Ronaldo missed a penalty and Portugal blew the chance to finish top of Group B, with a dramatic climax seeing them draw 1-1 with minnows Iran.
The European champions were leading through Ricardo Quaresma’s stunning goal but were dramatically pegged back in stoppage time by a controversial spot-kick – and they would have gone out of the tournament had Mehdi Taremi not missed a golden chance at the end.
Ronaldo should have ended doubts about the outcome, and taken his goal tally for the tournament to five when Portugal was awarded a 51st-minute penalty in Saransk.
But the Real Madrid superstar suffered a rare blip as he saw his effort beaten away by Iran goalkeeper Ali Beiranvand.
Things almost got worse for Ronaldo when he was the subject of a lengthy red card review by the video assistant referee, but his apparent swing of the arm was adjudged only worthy of a yellow.
But his night ended still in huge disappointment after Karim Ansarifard equalised from the spot in stoppage time.
Spain, meanwhile, will face hosts Russia in the last 16 of the World Cup after finishing top of Group B following a dramatic 2-2 draw with already-eliminated Morocco in Kaliningrad.
Spain, 2-1 down thanks to Morocco substitute Youssef En-Nesyri’s 81st-minute header, was set to finish second and therefore take on Uruguay in the next round.
Their substitute Iago Aspas then flicked the ball into the net in the dying seconds and saw that the flag was up for offside – but the goal was subsequently given following a VAR review.
Spain had gone 1-0 down in the 14th minute when Khalid Boutaib punished a mix-up between Andres Iniesta and Sergio Ramos, before drawing level five minutes later through Isco’s strike.
While Fernando Hierro’s side – who survived a scare 10 minutes after the break as Nordin Amrabat’s strike hit the woodwork – snatched a point, Portugal was held to a 1-1 draw by Iran thanks to a late goal by their opponents.