City stunned the Bernabeu when substitute Edin Dzeko raced clear to make it 1-0 in the 69th minute before Marcelo equalised eight minutes later with a deflected effort.
The English champions were ahead again five minutes from time when Real goalkeeper Iker Casillas failed to deal with Aleksandar Kolarov's inswinging free kick but the home side snatched victory in a frenetic ending.
Karim Benzema made it 2-2 in the 87th minute with a low shot that sneaked inside the post before Ronaldo cut in from the left and his powerful drive flew into the net off goalkeeper Joe Hart to send the crowd into raptures.
"It's not on. You can't go 2-1 up and lose the game," Hart told ITV.
"We can only blame ourselves. They are a very good team and so are we. We dug deep, got a lead twice and threw it away. It's hard to come off the field after losing 3-2 and be positive."
Real were under pressure to produce a convincing European performance after a stuttering start to their La Liga campaign and made their intentions clear early when Ronaldo forced two fine saves from Hart.
City barely had a sniff of goal in the first half, while Real striker Gonzalo Higuain fluffed two clear chances, one when he was clean through with only Hart to beat and the other when he volleyed over from close range.
Twice in the second period it looked as if City might pull off a smash and grab raid in the home of the nine-times winners but each time Real found an answer.
Ronaldo's dramatic late winner prompted wild celebrations among the Real faithful and under pressure coach Jose Mourinho performed a celebratory knee slide onto the pitch.
"It is a good win because the DNA of my team is back," Mourinho said.