The modest southern Madrid club had held on to fourth place for 11 of the last 12 rounds, but it will go into next weekend in fifth place and needing some help to earn its berth in the European competition for the first time in its 36-year history.
Valencia overtook Getafe for fourth place with a 3-1 home win against Alaves. The teams are level on 58 points, but Valencia is ahead on the head-to-head tiebreaker.
Sixth-place Sevilla still has a chance to earn the final Champions League spot after a 1-1 draw at Atletico Madrid. It will need to win its final match against Athletic Bilbao at home and hope for setbacks by Valencia at Valladolid and Getafe against Villarreal.
With Barcelona having already secured the title, Atletico clinched its second straight second-place finish with the draw against Sevilla.
Real Madrid ended third after a 3-1 loss at Real Sociedad, a result that kept the team winless in five away matches since coach Zinedine Zidane returned.
Villarreal, Levante and Valladolid escaped relegation, with Celta Vigo and Girona still trying to avoid the final demotion spot.