Santiago Solari's side looked on track for a sixth straight victory in the league following Casemiro's first-half header at the Santiago Bernabeu on Sunday.
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 LaLiga 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 goal 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' 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.
100% - Of players to have scored each of the penalties they have taken in @LaLigaEN since the turn of the 21st century, Girona's Cristhian Stuani has scored more than any other player in the competition (13/13). Lethal. pic.twitter.com/nKili9QyKI— OptaJose (@OptaJose) February 17, 2019
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 LaLiga 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 Champions League in midweek - available for the second leg of the Copa del Rey semi-final against Barca, as well as the league 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 facts
- Karim Benzema has reached 300 LaLiga appearances. He is the second French player to achieve the milestone, Antoine Griezmann the other.
- Casemiro has scored in back-to-back LaLiga games for the second time in his career.
- No LaLiga 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 LaLiga career - he is just one away from matching Manolo Alfaro's record for the competition.
- Girona are the only visiting side in LaLiga 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's LaLiga trip to Levante. Girona, meanwhile, return to action the following day at home to Real Sociedad.