Two goals in the space of four second-half minutes overturned Isco's opener as the hosts claimed one of the most famous victories in their 87-year history.
Played against a tense political backdrop in Catalonia, Girona started brightly as their intensity and enthusiasm appeared to unsettle the champions.
Zinedine Zidane's side looked to have calmed the storm when Spain midfielder Isco gave them a 1-0 lead at the break, but significant cracks in defence were soon pried open by the promoted side.
Cristhian Stuani equalised with his fifth league goal of the season before attacking midfielder Portu put Girona within touching distance of a victory in their first ever competitive clash against Los Blancos.
And Pablo Machin's men held on to end a three-match losing run at home in LaLiga in improbable fashion, leaving the visitors eight points off leaders Barcelona after just 10 matches.
Zidane recalled a host of first-team stars who sat out the midweek Copa del Rey win at Fuenlabrada, as only Kiko Casilla and teenager Achraf Hakimi held their places.
Girona were hardly overawed and went agonisingly close to a shock lead when Pablo Maffeo's curling effort came back off the left upright.
The hosts' frustration was immediately compounded by a blistering Madrid counter-attack. Goalkeeper Bono spilled Cristiano Ronaldo's dipping shot at the end of the move, enabling Isco to bury the close-range rebound for a 12th-minute lead.
Despite their advantage, Zidane's men looked shaky at the back as they regularly gave away possession in dangerous areas.
And the woodwork proved their saviour a second time, Casilla this time indebted to his right post for keeping out Portu's superb looped header late in the half.
Girona finally got their reward in the 54th minute when Pere Pons' incisive dribble broke for Stuani, who skilfully stepped past his marker and finished clinically into the bottom near corner.
Madrid's back four, which lost Raphael Varane to an apparent leg injury at half-time, was completely scattered by that stage and shipped a second just four minutes later.
Casilla saved with his feet from Stuani before Portu, possibly in an offside position, intelligently flicked home Maffeo's follow-up attempt to complete the comeback.
It could have been 3-1 with just over 20 minutes remaining, but Stuani guided his finish narrowly wide following Alex Granell's smart through pass.
The miss did not matter, though, as the introduction of Marco Asensio and Lucas Vazquez failed to deny Girona a famous victory.