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 were 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.
A 1-1 draw condemned Portugal to second place, behind Spain, and they will face Uruguay in the second round.
Quaresma, starting his first World Cup match, had earlier scored a memorable goal on the stroke of half-time.
Quaresma played a quick one-two with full-back Cedric Soares, sprinted to the edge of the box and curled the ball with the outside of his right boot into the far top corner.
Ronaldo's big moment arrived via VAR, after he was brought down in the area by Morteza Pouraliganji's clumsy challenge.
Referee Enrique Caceres initially gave nothing but after a second viewing pointed to the spot.
Yet Ronaldo was denied by a fine save from Beiranvand, diving to his right to beat the ball away.
It was a save in sharp contrast to the keeper's first-half performance, when he kept dropping crosses and was involved in a defensive mix-up which almost gifted Portugal an opening goal.
Fortunately for Beiranvand, Joao Mario could only lift the loose ball over the top.
But it was a poor first half from Portugal, who could have fallen behind had Saeid Ezatolahi placed his header either side of Rui Patricio, rather than straight at the goalkeeper.
Ronaldo's World Cup was in the balance with eight minutes remaining after he caught Pouraliganji off the ball.
But after a long, hard look at the replay Caceres returned to the pitch and showed the Portugal captain the yellow card.
The drama continued deep into added time with yet another VAR referral and Soares harshly adjudged to have handled in the area.
After Ansarifard had tucked away the penalty Portugal were suddenly in danger of exiting the tournament - and they would have if Taremi had found the target, instead of the side-netting.
- Press Association & Digital Desk