Looping cross
Nabil El Zhar headed home a looping cross in the 52nd minute and 20-year-old Oscar Rodriguez turned in a second goal barely sixty seconds later to the joy of the 12,000 fans crammed into the Butarque stadium.
Barca's unexpected loss handed Real the chance to move three points clear at the top of the table but the European champions were outclassed by a rampant Sevilla, who scored all their goals in a livewire first half to inflict a first league defeat on Real under coach Julen Lopetegui.
Wretched night
Portuguese striker Andre Silva gave Sevilla the perfect start by scoring from close range in the 17th minute after a mix-up between Real's out-of-form defender Marcelo and midfielder Casemiro.
AC Milan loanee Silva pounced four minutes later after poor goalkeeping from Thibaut Courtois, netting for the sixth time this season and moving above Lionel Messi as the top scorer in Spain.
After Franco Vazquez hit the bar, striker Wissam Ben Yedder scored in the 39th minute for his sixth goal in a week, rounding off a remarkable fourth consecutive home league win over Real for the Andalusian side.
Real's wretched night continued as Luka Modric had a goal ruled out for offside after a lengthy consultation with the video assistant referee (VAR) and Marcelo came off injured, endangering his chances of playing in Saturday's city derby against Atletico.
Superior side
The timing of Marcelo's injury also went against Real, who had to play the final 20 minute with 10 men as Lopetegui had already made the maximum three substitutions.
"We came here with the aim of putting pressure on Sevilla and we couldn't do it as we wanted to, that's obvious, and Sevilla were the far superior side," said Lopetegui.
Unlikely win
Barca coach Ernesto Valverde, meanwhile, will face criticism for rotating his side again after resting players in Sunday's surprise 2-2 home draw with Girona.
For the trip to the suburbs of Madrid, Valverde brought in Thomas Vermaelen at the expense of Jordi Alba to deploy an unfamiliar back three and started without striker Luis Suarez to give forward Munir El Haddadi a first competitive start since returning on loan from Alaves.
Vermaelen was caught out of position when El Zhar headed in the equaliser and Gerard Pique was caught in possession moments later, allowing Rodriguez to slam the ball into the roof of the net and give Leganes, in their third season in the top flight, an unlikely yet thoroughly deserved win.