Gerard Moreno struck deep in stoppage-time to give Espanyol a dramatic 1-0 win over Real Madrid at RCDE Stadium on Tuesday.
A much-changed Real Madrid, with Cristiano Ronaldo left out of the squad, looked to have done enough to labour to a share of the points.
However, with more than two minutes of added time played, Espanyol broke clear and Moreno crashed the ball past Keylor Navas to give the Barcelona club a first win over Real Madrid since 2007.
The result ended Espanyol's 22-match winless streak against Real, while also snapping Los Blancos' 11-game winning streak against the Barcelona-based club.
Madrid were looking to build on a seven-match unbeaten run in La Liga, which had seen them pick up six victories and score 30 goals. Instead, they lost a fifth game this season after only losing three times in the 2016-17 campaign.
Head coach Zinedine Zidane opted to make five changes from Sunday's 4-0 home win over Alaves, with captain Sergio Ramos back from suspension.
Gareth Bale retained his place, and the Wales forward almost opened the scoring on seven minutes.
However, his close-range header flew straight at Espanyol keeper Diego Lopez after right-back Achraf Hakimi had got on the overlap.
Raphael Varane's header from a corner was just off target as Real continued to dominate the opening exchanges.
Pablo Piatti fired well over before striker Moreno was denied by Navas, who threw out a leg to deflect his angled shot wide.
Espanyol had the ball in the net just after the half-hour, but Moreno's finish from a long-range free kick by Carlos Sanchez was offside in what replays showed to be a debatable decision.
At the other end, Marcos Llorente pulled a low effort wide as again Real failed to make the most of their possession.
Bale wanted a penalty just before half-time when his shot cannoned off Victor Sanchez, but the referee was not interested in the appeal for what would have been a harsh handball decision against the Espanyol midfielder.
Early in the second half, Navas had to make a smart save at the near post from Sergio Garcia's angled effort.
From the resulting corner, Real were fortunate not to go behind as Sergi Darder's header looped onto the top of the crossbar.
With 20 minutes left, Karim Benzema, who netted a late penalty against Alaves, replaced Isco.
Navas again needed to be alert when producing a smart low stop down to his left from Moreno's header.
The Real Madrid keeper, though, could do little to prevent Espanyol snatching a dramatic winner deep into stoppage time.
Moments after Ramos, who had been pressed forward by Zidane, was just unable to get on the end of a low ball across the face of goal, the hosts broke quickly with the ball worked out to Garcia on the right.
Garcia pulled the ball back towards the edge of the penalty area, where Moreno arrived to crash it past Navas with almost the last kick of the game and seal Espanyol's first win over Real Madrid in some 22 matches.
Defeat leaves Real Madrid in third place, 14 points behind leaders Barcelona, who have a game in hand and two ahead of Valencia, but having played one match more.