Cristhian Stuani scored his 13th La Liga goal of the season and Portu headed a famous winner as relegation-threatened Girona claimed a 2-1 comeback win away at Real Madrid.
Santiago Solari's side looked on track for a sixth straight victory in the league after Casemiro's first-half header at the Santiago Bernabeu.
But after Sergio Ramos was punished for a handball in the penalty area, Stuani stepped up to convert a spot-kick and Girona's battle against relegation was boosted by a stunning winner.
Thibaut Courtois weakly parried Anthony Lozano's effort back into the danger zone and Portu was on hand to steer the rebound past the Madrid goalkeeper, with captain Ramos then dismissed in the last minute as he tried to equalise with an overhead kick.
Madrid took the lead in the 25th minute with Casemiro on target for the second La Liga game in a row, the Brazil midfielder powering home a downward header from a Toni Kroos cross.
Casemiro teed up international team-mate Marcelo for a drilled finish before the break, but the effort was rightly ruled out for offside.
Girona should have equalised in the 59th minute. Courtois could only push Lozano's header back into the danger zone but Aleix Garcia blazed the rebound way over the crossbar.
However, Madrid failed to heed that warning and Girona levelled from the penalty spot.
Courtois turned Stuani's effort on to the post but Douglas Luiz got to the loose ball first and his shot was partially blocked by Ramos's arms.
Stuani clipped the spot-kick down the middle after Courtois dived to his right to equalise and Girona suddenly had Madrid reeling, Portu crashing a rocket of a shot against the post.
And Girona scored an incredible winner in the 75th minute, Portu squeezing a header past Courtois to complete a remarkable comeback.
Substitute Gareth Bale was denied by Yassine Bounou and as Madrid chased a leveller, Ramos was shown a second yellow card after referee Guillermo Cuadra Fernandez deemed his acrobatic effort to be a dangerous challenge.
Courtois almost made amends with an unbelievable 95th-minute equaliser but, after going up for a corner, his header flew wide in a frantic finale.
What does it mean? Madrid lose their focus
After a sticky spell under Solari, Madrid had looked back to their irresistible best.
But they missed the chance to move back up to second place in La Liga as a result of their loss, with Los Blancos now having a nine-point gap to make up on league leaders Barcelona.
At least they will have Ramos - who denied collecting a deliberate booking away to Ajax in the UEFA Champions League last week - available for the second leg of the Copa del Rey semi-final against Barca, as well as the league El Clasico in early March. He will be suspended for their next game against Levante instead.
Eusebio changes might save his job
With Madrid seemingly cruising to victory, Girona coach Eusebio Sacristan may have been on the brink of the sack with his side heading towards a seventh consecutive loss.
Eusebio sent on Lozano and Garcia at the break and his double change got Girona - who were knocked out of the cup by Madrid in January - on the front foot. It was their first ever win at the Santiago Bernabeu.
Courtois struggles after shaky start
Madrid goalkeeper Courtois was at fault for Girona's winner and he had a miserable day all round. He was left with a bloody nose after blocking a Stuani shot in the second minute and did not appear to recover his composure after needing treatment.
Key Opta stats:
- Karim Benzema has reached 300 La Liga appearances. He is the second French player to achieve the milestone, Antoine Griezmann the other.
- Casemiro has scored in back-to-back La Liga games for the second time in his career.
- No La Liga team scored more headed goals than Real Madrid (15) in all competitions this season.
- Stuani has scored all 14 penalties he has taken in his La Liga career - he is just one away from matching Manolo Alfaro's record for the competition.
- Girona are the only visiting side in La Liga to have collected points at both the Santiago Bernabeu and Camp Nou this season.
Madrid have a rare clear midweek with no games until next Sunday evening's (Monday morning AEDT) La Liga trip to Levante. Girona, meanwhile, return to action the following day at home to Real Sociedad.