Spain snatched a place in the Nations League final four after striker Alvaro Morata's late goal secured a 1-0 win over hosts Portugal on Tuesday to ensure top spot in their group.
Portugal dominated the match but last year's finalists Spain stayed in the contest and in the 88th minute Morata scored to silence most of the sold-out stadium after a long ball into the box from Dani Carvajal was headed across goal by Nico Williams.
It was Spain's first competitive win in Portugal since 1934 and their first victory in any match in Portugal since a 3-0 success in a friendly in 2003.
Spain finished top of League A-Group 2 on 11 points from six games, one point above second-placed Portugal who only needed a draw to secure a spot in the semi-finals.
The Spaniards join Croatia, Italy and the Netherlands in next year's finals tournament.
"If we were knocked out it had to be by leaving everything on the field and that's how it was one more time for us.
"Every time that Spain have to show up in big games we do it. That's how we do it and tonight we did it again."
It was a bitter blow for Cristiano Ronaldo's Portugal who created several chances, were in total control during most the game and came so close to securing a place in the finals.
The hosts almost opened the scoring in the 32nd minute through Diogo Jota, who received a long pass from Bruno Fernandes and lashed a shot towards the top right corner that Unai Simon kept out with a brilliant one-handed save.
Fernandes almost scored himself five minutes later when he rifled a cross shot into the side netting with an effort that was so close the home fans thought it was a goal.
Spain looked lost and confused in what was looking to be another lacklustre performance after they went down 2-1 at home to Switzerland on Saturday.
Spain manager Luis Enrique sprung a surprise with seven changes to his starting lineup, leaving Barcelona's Sergio Busquets, Gavi, Pedri and Jordi Alba on the bench.
It was not until he made four substitutions in the second-half that Spain woke up and got their first shot on target of the game after 70 minutes through Morata.
The Atletico Madrid forward almost scored in the 76th minute when he fired a long-range shot towards the bottom right corner that Diogo Costa kept out with an outstanding one-handed stop.
Ronaldo tried hard to score but his play was often erratic and he was unable to successfully link up with his team mates, who kept missing chances and left Spain in the game, allowing Morata to be the hero again with his late winner.
Portugal manager Fernando Santos said the loss did not change his team's confidence and preparations for the World Cup, where he believes Portugal "are still one of the favourites." They face Ghana, Uruguay and South Korea in Group H.
"We were really well organised in the first half. They had possession but didn't create anything with that. However, we went down and lost the intensity after the 60th minute," Santos told a news conference.
"They grew mainly because we lost the ability to have the ball. Players could not turn on their game.
"Spain began to push and came from really having no chances to end up scoring a goal. In the final minutes they changed the script, that's evident."
Spain will take heart from the win as they prepare to play Costa Rica, Germany and Japan in Group E at the World Cup.