Real Madrid paid the price for a monotonous performance as Gerard Moreno's last-gasp volley gave Espanyol a first Liga win against Los Blancos in 11 years.
Espanyol's ambition was rewarded, having looked the more dangerous team on the night, while the result all-but-ends Madrid's slim hopes of a title defence.
Madrid rested Cristiano Ronaldo for the trip to Catalonia, but Sergio Ramos, Marco Asensio and Isco all returned to the line-up, while the in-form Lucas Vazquez stayed out on the right against his former club.
You would have to go back to October 2007 for the last time Espanyol beat Los Blancos in the top flight, yet they came closest to taking the lead early on.
Gerard found the back of Keylor Navas' net after he blocked Carlos Sanchez's long-range volley, but the linesman deemed the striker offside.
Raphael Varane's poor clearance gifted Gerard a second bite of the cherry, but his angled finish across Keylor flew the wrong side of the far post.
Madrid had a couple of chances themselves, but Gareth Bale's header from a yard out failed to beat Diego Lopez, before Varane nodded a corner just wide.
That trend continued into the second half, with a looping header clipping Keylor's crossbar and Sergio Garcia's volley swerving inches wide of the Costa Rican's top-right corner.
Los Merengues' experiment of Bale playing as a lone striker simply didn't work, yet neither the introduction of Karim Benzema nor a switch to two strikers were able to spark them into life.
As the final whistle loomed, Gerard curled one just wide from close range but went one better with the last kick of the game, smacking a volley home first time from a pinpoint delivery.
Espanyol 1-0 Real Madrid
Espanyol: Diego Lopez; Marc Navarro, David Lopez, Oscar Duarte, Aaron; Darder, Carlos Sanchez, Victor Sanchez (Granero 70), Piatti (Baptistao 80); Sergio Garcia, Gerard
Real Madrid: Keylor; Achraf, Varane, Ramos, Nacho; Kovacic (Mayoral 89), Marcos Llorente; Vazquez, Isco (Benzema 69), Asensio; Bale (Ceballos 81)
Referee: Sanchez Martinez