Spain needed a goal in stoppage time here Monday to pull off a 2-2 draw with Morocco and finish first in Group B at the 2018 World Cup.

Though Portugal finished with the same number of points, 5, La Roja take the top spot by virtue of goal differential and will play Group A runner-up Russia next Sunday in Moscow.

Khalid Boutaib put underdogs Morocco ahead 1-0 in the 14th minute of the contest at Kaliningrad’s Arena Baltika, exploiting a miscue between Sergio Ramos and Andres Iniesta to score his side’s first goal of the tournament.

The Spaniards pulled level five minutes later with a strike from Isco Alarcon to culminate an extended give-and-go with Iniesta.

Boutaib nearly got another in the 25th minute, but keeper David De Gea was prepared this time and made the stop.

Spain dominated possession without getting much penetration and De Gea was called on again at the hour-mark to preserve the deadlock, as the scrappy Moroccans continued to generate more chances than their more-talented opponents.

So it was no surprise when Morocco got a second goal, courtesy of Youssef En-Nesyri in the 81st minute.

La Roja drew even in the 91st, with some help from VAR, which confirmed that Iago Aspas was onside when he tucked the ball inside the post.

Meanwhile, Spain and Portugal have made the cut from Group B to the Round of 16.