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 on Monday (NZDT).
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 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.
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 set piece, his header flew wide in a frantic finale.