There was late drama in Group B as stoppage-time equalisers for Spain and Iran saw the former top the standings at Portugal's expense.
Spain had appeared set for second as they trailed 2-1 against Morocco, but Iago Aspas drew them level with the last kick of the game just before Portugal conceded a penalty which Iran's Karim Ansarifard converted.
The results mean Spain progress as group winners to face Russia in the round of 16, with Portugal left to take on Uruguay - yet to concede a goal at the World Cup.
Sky Bet make Spain their 4/1 favourites to lift the trophy with Portugal quoted at 22/1, marginally shorter than their last-16 opponents.
Spain, 2-1 down thanks to Morocco substitute Youssef En-Nesyri's 81st minute header, were set to finish second and therefore take on Uruguay in the next round.
Their substitute 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 were held to a 1-1 draw by Iran thanks to a late goal by their opponents.
Cristiano Ronaldo missed a penalty as his side blew the chance to finish top of the group.
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 Ansarifard equalised from the spot in stoppage time.
It leaves Spain and Portugal level on five points each in first and second place, with goals scored putting the former ahead.
Opta Facts
Spain v Morocco
- Spain remain unbeaten in 23 matches (W15 D8) since losing to Italy at Euro 2016.
- Spain have failed to win their final group game of a World Cup tournament for the first time since 1982, having won each of the previous eight such games before today.
- Spain have finished the group stages unbeaten for the first time since 2006, when they won all three matches, losing at least once in each of the last two editions of the competition (2010 and 2014).
- Spain are now winless in each of the last four World Cup games in which they have conceded first, losing two and drawing two.
- Morocco have now won just one of their last 10 World Cup matches (D2 L7).
- Sergio Ramos made his 16th appearance for Spain at the World Cup in this game, overtaking Xavi as their most capped outfielder in the competition.
- Iago Aspas has been directly involved in 10 goals in his 10 appearances as a substitute for the Spanish national team (six goals, four assists).
- Khalid Boutaïb’s goal for Morocco was his side’s 29th shot at this year’s World Cup, and their first goal in the competition since 1998 when Salaheddine Bassir scored their final goal in a 3-0 win over Scotland.
- Isco has been directly involved in 12 goals in his last 15 international matches for Spain (10 goals, 2 assists).
- Despite not scoring against Morocco, Diego Costa is just the sixth Spanish player to score at least three goals in the first-round group phase of a World Cup, and the first since David Villa in 2010.
Iran v Portugal
- Portugal have progressed from the group stage of the World Cup for the fourth time in their history (also 1966, 2006 and 2010).
- Iran have been eliminated from the group stage at the World Cup in all five tournaments they’ve competed in (1978, 1998, 2006, 2014 and 2018).
- Both of Iran’s goals at the 2018 World Cup were scored in injury time (94:06 against Morocco, 92:48 against Portugal).
- Portugal’s Ricardo Quaresma (34y 272d) is the oldest player to score on his first World Cup start since Yahya Golmohammadi (35y 84d), who did so for Iran in 2006 against Mexico.
- Iran are the second Asian nation in World Cup history to have won their first match at a World Cup but be eliminated at the group stage, after South Korea in 2006.
- Two of Cristiano Ronaldo’s three penalties at the World Cup for Portugal have been against Iran – he scored in 2006 but saw his effort saved in this game.
- Cristiano Ronaldo has a 50% penalty success rate in major tournaments for Portugal, scoring two (against Iran and Spain at the World Cups in 2006 and 2018 respectively) but also failing with two (against Austria at Euro 2016 and Iran today).
- Iran remain winless in eight World Cup matches against European opposition (D2 L6).
- Iran’s goal – scored after 92 minutes and 48 seconds – was the latest goal Portugal have ever conceded at the World Cup.
- Portugal have never lost a World Cup match when they’ve scored first (W13 D3).