A run of five straight wins, including last weekend's 3-1 defeat of city rivals Atletico at the Wanda Metropolitano, had thrust Madrid back into the title race ahead of the Clasico on March 2.
Casemiro's header put the hosts on track to extend their streak before Girona produced a stunning second-half comeback at the Santiago Bernabeu on Sunday, however.
After Sergio Ramos was deemed to have blocked a shot with his arm in the box, Cristhian Stuani beat Courtois from the penalty spot to score his 13th La Liga goal of the season.
And when Courtois pushed Anthony Lozano's effort back into the danger zone, Portu squeezed his header past the prone Madrid goalkeeper to claim a famous win.
Ramos was then dismissed after an attempted overhead kick in the Girona area, with Courtois going up for a 95th-minute corner but seeing his header bounce off
"I decided to go up for the corner to try and create some nerves," Courtois said in a post-match interview. "I got a touch on it but it just went wide.
"I think the first half we played really well, dropped the rhythm a bit and weren't at the same level as against Ajax or Atletico but we were in charge.
"In the second half they found a groove and created danger, I don't know if it was a penalty, I didn't see it, but it was a shame. They equalised and we didn't know how to control it.
"The shot came in that I couldn't control and they scored the winner. It's a shame as we needed to win. It's not a question of confidence. Girona always
Girona had not won any of their
Eusebio Sacristan was reportedly on the brink of the sack but Girona, who earned a draw away to Barcelona in September,
"It's a historic win and the team needed it after a complicated moment with a really bad run of results," Portu said. "It was a real challenge but above all this team has great spirit and can do great things.
"The key to all this was the group spirit there is. We knew it would be complicated and we couldn't dream of winning the points. But in the second
"We played well despite being 1-0 down. We showed the important thing is what is inside the group and the union we have with our technical staff. In the difficult
"At the end of the day I'm not even thinking about who scored, we needed the three points and we can breathe a bit now. We'll be celebrating like mad as we deserve this."