Barcelona absolutely had to beat Sevilla if they were to stay in the title race.
But Leganes scored twice in quick succession through Gabriel Appelt Pires and Luciano to cut the deficit to 3-2 at halftime.
They started off strong, as Lionel Messi's first shot of the game smacked off the crossbar inside the opening four minutes.
Brazil global Neymar took a back seat as Barca recorded a 3-0 LaLiga win over Sevilla on Wednesday, Messi scoring a first-half brace after Luis Suarez's opener.
Madrid coach Zinedine Zidane hopes they can pick up the pace after a good result though disappointing display at the Bernabeu.
If in the first half of the championship, both chances of winning the match were approximately equal, then already in the upcoming game, Barcelona remains the absolute favorite. "We arguably even deserved to have a bigger lead". That's why we always need to be looking to improve.
The visitors quickly restored their two-goal cushion just three minutes into the second half when Leganes captain Martin Mantovani contrived to punch the ball into his own net.More news: Republicans Take First Congressional Election of Trump Era
It leaves Jorge Sampaoli's side 11 points behind Barca and without a win in six games, while Luis Enrique's men are keeping the pressure on league leaders Real Madrid.
To make matters worse, winger Vitolo was sent off for a second bookable offence late in the game when he hacked down Neymar and while Sevilla are probably safe in terms of Champions League qualification, third-placed Atletico Madrid are now three points ahead of them.
The Argentinian is now La Liga's scoring leader with 25 goals, he's tallied 41 goals overall this season in 40 matches.
Messi, who had missed Sunday's win in Granada through suspension, strike once again and had Barça out of sight after just 33 minutes when he rifled in a loose ball in the box.
Luis Suarez may have stolen all the headlines against Sevilla after that goal but there was another performance at Camp Nou on Wednesday that left Luis Enrique purring.
Given what has become a rare start against the Andalusians, the midfielder starred in a strong first-half display and yet the coach can not guarantee his position in the XI at this stage of the season. "As a striker you have a notion of where the goal is and it managed to help the team win", Suarez told BeIN Sports.