Valencia fought back twice to earn a Champions League berth with a 3-2 away win that relegated Almeria on a dramatic final day of the Spanish league Saturday.
Eibar also went down despite beating last-place Cordoba 3-0, while Deportivo La Coruna stayed up after coming from two goals down to draw 2-2 at already crowned champion Barcelona. Lionel Messi had put Barcelona in front with his 42nd and 43rd league goals of the season.
''Our supporters have a song that says this team never surrenders and that's what happened,'' said Deportivo coach Victor Sanchez del Amo. ''We had the capacity to respond and this is the recompense for a job well done.''
Cristiano Ronaldo scored a hat trick to lead runner-up Real Madrid to a 7-3 rout of Getafe as the Ballon d'Or holder finished as the league's top scorer with the outstanding mark of 48 goals. That left him two goals shy of Messi's milestone from 2012.
Granada was rescued by a 0-0 draw with Atletico Madrid, which finished in third place a year after winning the league title.
Sevilla had to settle for a fifth-place finish despite beating 10-man Malaga 3-2. Unai Emery's side can still reach the Champions League by successfully defending its Europa League title against Dnipro in Warsaw on Wednesday.
Athletic Bilbao is bound for the Europa League along with Sevilla and Villarreal after securing seventh place with a 4-0 rout of Villarreal.
Emotions ran highest at Almeria where the hosts twice scored to go ahead and provisionally escape the bottom three spots.
But Valencia's Nicolas Otamendi answered Thomas Partey's opener from a free kick, and Sofiane Feghouli struck just before halftime to cancel out Fernando Soriano's goal.
After losing goalkeeper Diego Alves to a left-leg injury, Valencia needed Paco Alcacer to run onto a long pass and fire the ball beyond goalie Ruben Martinez with 10 minutes left to clinch a top-four finish and a ticket to Europe's top-tier competition.
''My players knew how to handle the pressure,'' said Valencia coach Nuno Espirito Santo. ''Valencia has succeeded in earning a place among the best, which is where it should be.''
At Camp Nou, Barcelona's fans paid homage to Xavi Hernandez, who played his last league game after 17 seasons at the club. The midfielder is leaving the club for Qatar this summer, and supports held up an enormous banner that read: ''Thanks Xavi, 17 seasons 1998-2015.''
Xavi then received a standing ovation when he was replaced by Andres Iniesta with five minutes remaining. He responded by blowing kisses to the stands before showing some tears in the dugout after taking his club record to 765 appearances.
Xavi will get one more chance to play at Camp Nou next weekend and add to his club record 23 titles when Barcelona hosts Bilbao in the Copa del Rey final. Barcelona will then play Juventus for the European Cup in Berlin on June 6.
Barcelona received an honor guard from Deportivo for its 23rd league crown, and the visitors seemed ready to roll over after Messi headed in Rafina's cross in the fifth minute and tapped in Neymar's pass to make it 2-0 in the 60th.
But Lucas Perez gave Deportivo a lifeline seven minutes later with a rising strike from just inside the area, and Diogo Salomao got the goal they were desperately searching for in the 76th.
It was a double jubilation on the Camp Nou pitch after the game as Deportivo's players celebrated staying up, before Xavi received the league trophy and presented it to the crowd.
''I want to leave with the treble. We want the Liga, we want the Copa and we want the Champions,'' Xavi told Barcelona's supporters. ''Thanks for everything. You have made me the happiest man in the world.''
Elsewhere, Real Sociedad thrashed Rayo Vallecano 4-2, 10-man Celta Vigo rallied to beat Espanyol 3-2, and Elche drew 0-0 at Levante.
Eibar manager Gaizka Garitano announced he was stepping down after the Basque club failed to stay in the topflight for a second season.
Madrid manager Carlo Ancelotti may be the next to go after a campaign without a major title for the powerhouse.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)