Alvaro Morata's late strike snatched Spain a 1-0 Nations League victory over Portugal in Braga on Tuesday and sent them into the semi-finals.
La Roja needed to win to finish top of League A Group 2, but struggled on another difficult night for the Euro 2020 semi-finalists, until Morata's 88th-minute breakthrough.
Spain have not been able to hit top form in the internationals leading up to the Qatar 2022 World Cup, but this hard-fought victory provides a welcome morale boost.
Portugal had the better of the game but Spain brightened in the final stages, with the introduction of several substitutes, including Nico Williams, who headed across goal for Morata to gleefully turn in at the back post.
"When we had to stand up, we did," Morata told TVE. "If we had lost, it had to be giving everything we had on the pitch, and that's what we did.
"I'll remember the attitude of the team, we fought until the end. And Nico Williams, in his second game for the national team, was key and I put it over the line."
Spain will join Italy, Croatia and the Netherlands in the semis in June 2023, although attention now switches to the fast-approaching World Cup.
Luis Enrique made wholesale changes to the side that suffered Spain's first home defeat since 2018 against Switzerland on Saturday, retaining only four starters.
Morata was brought in to lead the line, while the Asturian coach rotated his entire midfield trio.
Spain still dominated possession, as their gameplan demands, but to little effect as Portugal created the more dangerous openings.
Athletic Bilbao goalkeeper Unai Simon made a solid save to deny Ruben Neves and then a brilliant one to keep Liverpool striker Diogo Jota at bay.
With Portuguese confidence increasing, Bruno Fernandes lashed an effort narrowly off-target, celebrated prematurely by swathes of the stadium as the ball appeared to settle in the net.
Just as they did against Switzerland, Spain failed to get a single shot on target in the first half, and after Simon made another good save from Cristiano Ronaldo early in the second half, Luis Enrique took action.
The coach sent on Pedri, Gavi and Yeremy Pino to try and give La Roja more inspiration going forward, although it was the hosts who almost broke the deadlock when Dani Carvajal deflected a strike narrowly over his own crossbar.
The changes livened Luis Enrique's team up and they began to turn the tide, building momentum as the game reached its denouement.
Carvajal launched a crossfield ball into the area, with Williams' header leaving Morata with the simple task of firing into the empty net to put the runners-up of the last edition of the Nations League back into the final four.
An exasperated Ronaldo was denied by Simon at the death and Fernando Santos's Portugal left ruing their missed chances and wishing they had killed off Spain when they had the chance.
"We had many chances and Spain few, we are sad, we would have liked to have been in the finals," said Santos.
"Ronaldo had three or four chances that he would usually score, but he could not. That's football."
It was a disappointing end to a good run for Portugal, while Spain experienced the opposite sensation.
After defeat by Switzerland and frustrating draws with Portugal and the Czech Republic in June, Morata's late winner provided relief for La Roja.
"This is a wonderful sport in which winning is the best antidote to any depression or sadness," said Luis Enrique.
"Portugal are a top, top, top team. In the first half I insisted, maybe too much, on having the ball, and we did. It's evident we'd prefer to play the passes in their half.
"(But) the first half was necessary to show that the ball was ours, and in the second we had the sensation that the goal would come. We're in the final four again and it's a great joy."
In Nations League B, Serbia gained promotion after a 2-0 win in Norway. Scotland have also been promoted after a 0-0 draw with Ukraine. (AFP)