A late Spain goal combined with the result in the other Group B game, meant that La Roja topped the standings.
After 14 minutes though, Spain were behind after a loose touch from Andres Iniesta and hesitiation from Sergio Ramos allowed Khalid Boutaib to pounce and drive towards David De Gea’s goal and slot the ball through his legs into the back of the net.
Four minutes later though, Spain were level through Isco, as Iniesta dribbled his way into the box and squared the ball to the Real Madrid maestro, who slotted the ball into the roof of the net.
Boutaib had the opportunity to make it 2-1 to Morocco as he found himself one-on-one with De Gea again, but his final touch was too heavy and his attempted chip was straight at the Man United ‘keeper.
Ten minutes into the second half, Leganes loanee Nordin Amrabat released a thunderbolt of a shot, which left De Gea rooted, but the shot struck the underside of the crossbar.
Nine minutes from full-time and Morocco took a deserved lead from a corner, whipped in by Getafe midfielder Fayçal Fajr was met by Malaga forward Youssef En-Nesyri, who headed the ball into the corner past a watching De Gea.
90th minute, cue the drama. Daniel Carvajal’s low cross found Iago Aspas who flicked the ball into the net, which was initally disallowed for offside, but VAR came into the frame and it was the tightest of calls, however the Celta Vigo man was onside and the goal was given.
A late Iran penalty secured top spot for Spain, who will face Russia in the Last 16, whilst Ronaldo’s Portugal will go on to face Uruguay.