Substitute Iago Aspas struck in stoppage-time to earn Spain a dramatic 2-2 draw with Morocco on Monday which gave them top spot in World Cup Group B and a place in the last 16.
With Morocco heading towards their first win of the tournament, Aspas cleverly back-heeled the ball into the net to cancel out Youssef En-Nesyri's towering 81st-minute header for Morocco.
The Spanish equaliser was originally ruled out for offside but awarded after a lengthy review by the Video Assistant Referee (VAR) amid chaotic scenes on the pitch.
Spain took top spot from Portugal on goals scored after the European champions were held to a 1-1 draw by Iran who equalised in stoppage-time with a penalty also given after consultation with the VAR.
"There was a lot of uncertainty, we didn't know we had finished top until some team mates told me," Aspas said. "The important thing is that we are through and finished top of the group.
"Everyone would have taken finishing top of the group when the draw was made, then you have to think about everything that has happened (referring to the sacking of coach Julen Lopetegui), but we have just focused on working hard."
Spain will play Russia in the last 16.
"It will be tough game against the hosts, it will be a packed stadium," said Spain coach Fernando Hierro who took charge of the team two days before the tournament following Lopetegui's sacking.
"We managed to come top of our group,and we need to do our homework properly as Russia will be a very tough match."
The game was scrappy, Morocco picking up six yellow cards, and Spain lurched from indifferent to inspired throughout.
The first team eliminated from the World Cup following two 1-0 losses, Morocco started with intensity and Khalid Boutaid put them ahead in the 14th minute.
Snatching the ball from Sergio Ramos in midfield Boutaid ran through alone and coolly slotted home between the legs of David de Gea for Morocco's first goal of the World Cup and their first at the global showpiece tournament for 20 years.
It was a grey, rainy Baltic evening at Kaliningrad Stadium but the chill could not dampen the spirit of a fired-up pro-Moroccan crowd who celebrated the goal as if their team had just won the World Cup.
"We would have loved to get a win over Spain, this is why we came here tonight," Morocco coach Herve Renard said. "When you look at the lineup it's a mix of Real Madrid and Barcelona so these are outstanding players so our team should be commended.
"We lacked a bit of experience... it's because we haven't been in a World Cup for 20 years. But we showed we were able to take on two of the best teams in the world. The World Cup was a magical moment."
The goal shocked Spain into action and Isco equalised for the 2010 world champions five minutes later, taking Andres Iniesta's pass and crashing it into the roof of the net.
Spain nearly grabbed a second before halftime when Iniesta fired a low pass across the goalmouth that a sliding Diego Costa just failed to connect with.
Morocco returned with renewed purpose and Nordin Amrabat nearly gave the North Africans the lead in the 56th minute when his thunderbolt 25-yard shot clanged off the woodwork.
It was Spain's turn to go close when defender Romain Saiss cleared Isco's header off the line but Morocco struck a telling blow when En-Nesyri rose high to head home a corner.
Spain finally began to play with urgency but were frustrated until Aspas struck and his goal was only given after a long wait for VAR confirmation that he had not strayed offside.
"We leave the World Cup with our heads held high," Morocco midfielder Faycal Fajr said. "We gave everything, we defended our colours."Published: 2018-06-26 08:43:35