Spain will compete in next summer's Nations League finals after beating Iberian rivals Portugal 1-0 late in the day on Tuesday.
The Selecao only needed a point to qualify while Luis Enrique's Roja needed all three after their shock loss to Switzerland last week, and the visitors eventually found a way through to book their spot in the final four.
Portugal nearly gifted Spain an early opening Pablo Sarabia won the ball off of Joao Cancelo on the edge of the Selecao's penalty area, but his effort was blocked and the hosts got away with it.
Fernando Santos' men created their first half-sight of goal midway through the first half, with Diogo Jota heading back for Cristiano Ronaldo to tee up William Carvalho, but Spain had enough men behind the ball to make a block.
Ronaldo was furious that he wasn't awarded a penalty after a collision with goalkeeper Unai Simon, with the referee instead giving Portugal a corner, from which Ruben Neves stung the Athletic Club stopper's palms from range.
Simon had to be at his best to keep Jota out soon after when he was found by a searching through ball from Bruno Fernandes, with the Liverpool forward denied at the near post.
30,000 onlookers inside the Estadio Municipal de Braga thought Fernandes had found the top corner with an incredible strike from 25 yards, but the illusion soon proved that he had in fact found the side-netting instead.
A cross from Jose Gaya was looped up and over the bar by Ferran Torres and the two sides went into the break level.
After the interval, Jota stole possession from Dani Carvajal and set Ronaldo racing away, but Simon was quick off his line to thwart the Manchester United forward.
A Portuguese free-kick routine saw Fernandes pick out William Carvalho, who laid it off for Ruben Dias to fire, but Carvajal got an important touch on the strike to steer it over the bar.
The Selecao again won the ball back high up the pitch and Ronaldo looked ready to slot home from point-blank range, only for Gaya to make a tackle in time.
Substitute Nico Williams nearly scored with his first touches of the ball, but his curling shot was held by Diogo Costa.
Spain upped the ante in the dying embers of the match in search of a winner and grabbed an opener through Alvaro Morata, with Carvajal's cross for Williams nodded back into the path of the Atletico Madrid striker to tap in.
Ronaldo's half-volley from inside the area was kept out by Simon's legs in stoppage time and Enrique's men held on to top the group and ensure Spain head into the World Cup on a high.
Portugal vs Spain player ratings, Nations League
1. Portugal (4-2-3-1)
GK: Diogo Costa - 6/10 - Kept Spain at bay for much of the game but could do nothing about Morata's late goal.
RB: Joao Cancelo - 4/10 - Lacked the dynamism he so often show for Manchester City. Was fortunate that Spain didn't attack his side more often.
CB: Ruben Dias - 5/10 - Was far more uncomfortable in defence than his partner who's naturally a midfielder.
CB: Danilo Pereira - 6/10 - Clamped down Spain for the most part and looked a natural at centre-back.
LB: Nuno Mendes - 7/10 - Performed his duties well at both ends and brought fans off of their seats with his trickery.
CM: William Carvalho - 6/10 - Provided necessary protection in front of the defence but also kept play ticking.
CM: Ruben Neves - 4/10 - A little more careless in possession than his midfield partner.
AM: Bruno Fernandes - 6/10 - Pressed with intensity and was the creator of Portugal's best chances.
RW: Bernardo Silva - 4/10 - Played on the periphery and was understandably substituted.
CF: Cristiano Ronaldo - 5/10 - Looked more dangerous than he has all season but still lacked the finishing touches.
LW: Diogo Jota - 6/10 - The liveliest player on the pitch but couldn't find a goal for his efforts.
Manager: Fernando Santos - 5/10 - Portugal looked worthy of going toe-to-toe with another major rival, which they haven't done for a while.
SUB: Joao Mario (73' for Silva) - 5/10
SUB: Rafael Leao (79' for Jota) - 5/10
SUB: Vitinha (79' for Carvalho) - 5/10
SUB: Joao Felix (89' for Neves) - N/A
2. Spain (4-3-3)
GK: Unai Simon - 7/10 - Had a few hairy moments in possession but also made some crucial saves.
RB: Dani Carvajal - 5/10 - Given a rather rough evening by Jota and Ronaldo.
CB: Hugo Guillamon - 4/10 - Looked rather ropey in his one half of football. Hooked at the break.
CB: Pau Torres - 5/10 - His long-ranging passes were often inaccurate and if anything slowed Spain down even more.
LB: Jose Gaya - 7/10 - Kept Silva quiet and made an important challenge to stop Ronaldo.
DM: Rodri - 7/10 - Was competent in midfield and at centre-back.
CM: Carlos Soler - 4/10 - His only notable contribution was a horrendous shot from 30 yards which landed in the cliffs adjacent to the stadium. Subbed on the hour mark.
CM: Koke - 4/10 - Was also very ineffectual and too was hauled off by Enrique.
RW: Ferran Torres - 5/10 - Started brightly but faded very quickly.
CF: Alvaro Morata - 8/10 - After a quiet opening 70 minutes, Morata burst into life and came up trumps for his country.
LW: Pablo Sarabia - 4/10 - Cancelo was offering little to the game and Sarabia failed to capitalise on this.
Manager: Luis Enrique - 6/10 - Made seven changes from Spain's loss to Switzerland. The same flaws remained but his substitutions got the job done.
SUB: Sergio Busquets (46' for Guillamon) - 7/10 - Brought some much-needed composure and progression to the midfield.
SUB: Pedri (60' for Koke) - 7/10 - Spain were much better going forward because of his introduction.
SUB: Gavi (60' for Soler) - 7/10 - He too opened the game up and the Barcelona trio should probably have started.
SUB: Yeremy Pino (60' for Sarabia) - 5/10
SUB: Nico Williams (73' for F. Torres) - 8/10 - What a cameo. Was Spain's best player after coming on.