Real ended the campaign on 87 points after the draw interrupted a run of 10 consecutive victories, five clear of second-placed Barcelona.
Sergio Ramos headed home his 11th league goal this season to put Real in front after nine minutes, as Zinedine Zidane heavily rotated his team following the 2-1 win over Villarreal on Thursday that clinched their first Liga title since 2017.
Leganes levelled on the stroke of half-time through midfielder Bryan Gil but Marco Asensio restored Real's lead early in the second half.
Roger Assale equalised in the 78th to give Leganes a slither of hope of survival and they needed one more goal to pull off a miraculous escape from relegation.
They came agonisingly close to finding it, having a late penalty appeal turned down for handball against Luka Jovic after a VAR review.
Oscar Rodriguez then blasted over from inside the area deep in added time and the Madrid side dropped out of the top-flight for the first time since winning their maiden promotion to La Liga in 2016.
"Even though we didn't get the result we wanted, the sacrifice was worth it and we are proud to have fought until the end," said Leganes defender Unai Bustinza. "What we have done should be seen as an example that you should never give up."
Bustinza also reflected on the fact his side lost their two main strikers at the start of the year, forced to sell Youssef En-Nesyri to Sevilla and then Martin Braithwaite to Barcelona in February after their buy-out clauses were triggered.
He added: "This year has been an uphill struggle because as well as the lockdown period we didn't have that little bit of luck we needed and some very important players were taken away from us. We'll do our best to get back up."
Real have a crucial second-leg tie against Manchester City in the Champions League round of 16 next month, and Zidane turned his focus to the match after Sunday's game.
"The next thing we have to think about is the Champions League, the second leg, we know what happened in the first leg. Until the last second we'll fight to progress in the Champions League. But right now it's time to rest a bit, the final stretch has been a huge effort by everyone. We need to relax a bit now, we have a few days and that's what we'll do," he said of his side, who will carry a 2-1 deficit into the City match.
"I'm not worried, we didn't relax today. It wasn't a great game but I'll focus on what we did in all the games and the work we did until now... the opponent was playing their entire season today, and we weren't playing for as much. Now after four or five days of rest we'll return and work to think about the Champions League. No worries, no stress, nothing. We're thinking only about how good this season has been for us."