Match ends, Real Madrid 1, Girona 2.
Real Madrid missed the chance to go second in La Liga as Girona came from behind to record a surprise away win.
Casemiro powerfully headed in from Toni Kroos' deep cross from the right wing to give the hosts a first-half lead.
Cristhian Stuani's penalty made it 1-1 after Sergio Ramos' handball and Girona's Portu then hit the crossbar.
Portu netted the winner with a diving header after Anthony Lozano's shot had been parried, before Ramos was sent off in injury time for two yellow cards.
The defender had been booked when he conceded the penalty, and received a second caution when he attempted an overhead kick but caught Girona's Pedro Alcala.
The red card was the 19th of Ramos' career in La Liga, more than any other player in the Spanish top flight.
It was another controversial moment for the Real captain, who faces a Uefa investigation into whether he deliberately got booked late on in Wednesday's 2-1 win at Ajax in the first leg of their Champions League last-16 tie - to receive a suspension now and not later on in the competition.
Ramos had been quoted as telling reporters he would "be lying if I said I didn't force [the booking]", but later posted on social media that he had "not forced the card".
Changes backfire for Real boss Solari
Real, who started the weekend in second place in the table, are now third, nine points behind leaders Barcelona, who won 1-0 against Real Valladolid on Saturday.
Atletico Madrid are now second, two points clear of city rivals Real, following their 1-0 victory over Rayo Vallecano.
Real boss Santiago Solari made six changes to face Girona, with Wales forward Gareth Bale among the substitutes and midfielder Luka Modric missing through suspension.
Away keeper Yassine Bounou denied Lucas Vasquez before Casemiro's header gave the hosts the lead. Karim Benzema and Marcelo had further chances saved, and Benzema also headed into the side netting as Real pushed for a second.
However, the visitors - who had not won in their previous 13 matches - fought back. Aleix Garcia wastefully shot over from six yards out and Stuani headed against the post before Ramos charged down the follow-up shot with his hands and Stuani equalised from the spot.
Bale came on in the 67th minute, but was unable to prevent his side losing their grip on the match. Portu hit the crossbar and then scored a 74th-minute winner to severely dent Real's hopes of becoming Spanish champions this season.
'A fair result' - what they said
Real Madrid boss Santiago Solari said: "Girona were better in the second half, they caused us a lot of problems. We've been on a good run and today we did not do the same things.
"We tried to increase our energy levels with our changes but we did not achieve it - we have a big squad and a lot of good players.
"We did not take our chances when we had them and it was really difficult for us in the first 25 minutes."
Asked if they still had a chance of winning the league, Solari added: "Always. We are always in it. We need to get good results as soon as possible, but we're still in three competitions."
Girona manager Eusebio Sacristan said: "It's really important for us to win at the Bernabeu - for our fans, for our team.
"Maybe they were thinking they would get a second goal, but we were slowly getting back into it and getting more confident. At the end of the game it is a fair result.
"We had not won for a few games and had a tough few weeks. But we showed our attitude, and what better ground to do it in."
- 4RamosBooked at 90mins
- 24CeballosSubstituted forMarianoat 74'minutes
- 14CasemiroBooked at 88mins
- 17VázquezSubstituted forVinícius Júniorat 58'minutes
- 20AsensioSubstituted forBaleat 66'minutes
- 28Vinícius Júnior
- 5AlcaláBooked at 86mins
- 15Ramírez López
- 12Douglas Luiz
- 24PorroSubstituted forLozanoat 45'minutesBooked at 48mins
- 8PonsSubstituted forGarcíaat 45'minutes
- 6Granell Nogué
- 9Portugués ManzaneraSubstituted forFernándezat 84'minutes
- Guillermo Cuadra Fernández
- Shots on Target
Second Half ends, Real Madrid 1, Girona 2.
Attempt missed. Thibaut Courtois (Real Madrid) header from the centre of the box is close, but misses to the right. Assisted by Toni Kroos with a cross following a corner.
Corner, Real Madrid. Conceded by Jonás Ramalho.
Attempt missed. Mariano (Real Madrid) header from the left side of the box misses to the right. Assisted by Toni Kroos with a cross following a set piece situation.
Casemiro (Real Madrid) wins a free kick on the right wing.
Foul by Cristhian Stuani (Girona).
Offside, Real Madrid. Marcelo tries a through ball, but Gareth Bale is caught offside.
Attempt saved. Marcelo (Real Madrid) right footed shot from the left side of the box is saved in the bottom right corner. Assisted by Toni Kroos.
Corner, Real Madrid. Conceded by Pedro Alcalá.
Attempt blocked. Mariano (Real Madrid) header from the centre of the box is blocked. Assisted by Gareth Bale with a cross.
Delay over. They are ready to continue.
Delay in match Pedro Alcalá (Girona) because of an injury.
Second yellow card to Sergio Ramos (Real Madrid) for a bad foul.
Foul by Sergio Ramos (Real Madrid).
Pedro Alcalá (Girona) wins a free kick in the defensive half.
Sergio Ramos (Real Madrid) wins a free kick in the attacking half.
Foul by Douglas Luiz (Girona).
Attempt missed. Álex Granell (Girona) left footed shot from outside the box misses to the right from a direct free kick.
Casemiro (Real Madrid) is shown the yellow card for a bad foul.