Newly-crowned La Liga champions Real Madrid dropped points for the first time since the season's restart on Sunday in a dramatic 2-2 draw that was not enough to save Leganes from relegation.
Leganes, who needed to beat Madrid and hope Celta Vigo failed to win against bottom club Espanyol, almost pulled off a miraculous escape but could not find a winner at the Butarque.
The draw means a rotated Madrid side surrendered the perfect record of 11 victories out of 11 since Spanish football came back from its coronavirus-enforced suspension while Karim Benzema fell short of Lionel Messi in the battle for the golden boot.
Gareth Bale was left out of Madrid's squad completely for what might have been his last La Liga fixture for the club. Zinedine Zidane said afterwards it was a "techinical decision".
Messi had earlier scored twice in a 5-0 win over Alaves, enough for him to claim a record seventh 'Pichichi Trophy' and to give Barcelona a boost ahead of the return leg of their last 16 Champions League tie against Napoli next month.
The double took Messi's league tally to 25 goals for the season and put him four clear of Benzema, who endured a quiet night against Leganes, with Sergio Ramos and Marco Asensio on target.
The odds were always against Leganes but a miracle looked possible when Roger Assale equalised with 12 minutes left to find a winner.
But chances were missed and a penalty appeal was turned down, meaning they join Real Mallorca and Espanyol in Segunda next season.
"The lads are broken," said Leganes coach Javier Aguirre. "We had it within our grasp."
Real Sociedad and Granada, meanwhile, took the last two Europa League spots, with Getafe and Valencia missing out. Villarreal had already secured fifth place.
Granada thrashed Athletic Bilbao 4-0 and qualification caps an incredible season for the club, who were only promoted from Segunda last term.
"Our aim was to stay up so to finish in the Europa League is a dream for us," said Granada striker Roberto Soldado.
Real Sociedad snuck into sixth place after Adnan Januzaj's 87th-minute goal clinched a 1-1 draw away at Atletico Madrid.
- Barca believe in Euro glory -
Messi finishes as La Liga's top scorer for a record seventh time, pulling clear of Telmo Zarra's six while playing for Athletic Bilbao in the forties and fifties.
"I'm never that bothered about the individual prizes and I would have preferred it to have come with a league title like before," added Messi.
Setien said on Saturday he still believes Barcelona, at their best, can win the Champions League next month.
And a sparkling attacking display certainly gave a glimpse of what is possible, even if Napoli in the last 16, and potentially Bayern Munich in the quarter-finals, will offer a completely different test.
There could be fitness concerns for Setien too ahead of the second leg against Napoli. Clement Lenglet hobbled off with what looked like a groin problem, with Antoine Griezmann and Samuel Umtiti already out injured.
But Setien has increasingly been putting his faith in youth and the 20-year-old Puig impressed again in midfield while 21-year-old Ronald Araujo gave a composed, albeit largely unchallenged, performance in defence.
The 17-year-old Ansu Fati opened the scoring at the near post before Puig teed up Messi to stride through and finish.
Luis Suarez capped a superb move by heading in his 17th league goal after Jordi Alba had volleyed Messi's pass across, with another Puig through ball allowing Nelson Semedo to lash in a fourth.
Messi had the last word with a smart finish on the half-volley.
Madrid looked to have to have the game won when Asensio finished off Isco's pass but Leganes pulled level as Assale nipped in to set up a dramatic last 10 minutes.
With Celta still being held, they only needed a goal to survive and Javier Aviles might have got it only to miskick at the back post and then failed to connect from a corner.
Replays showed Luka Jovic touched the cross with his hand but referee Guillermo Cuadra was not convinced and even goalkeeper Pichu Cuellar going up for a 94th-minute corner could not give Leganes the goal they needed.
Real Madrid goalscorer Marco Asensio conforts Leganes players after a 2-2 wasn't enough to keep them in the Spanish top flight