Arturo Vidal had headed Bayern ahead but then wasted a chance to double their lead when he blasted a penalty over the crossbar.
And Ronaldo then twice clinically converted from crosses either side of Javi Martinez's red card to move to 97 Champions League goals.
Bayern were kept in the game after going down to 10 men by goalkeeper Manuel Neuer, who saved brilliantly from Gareth Bale, Karim Benzema and Ronaldo.
But the Germany international will feel he should have done better for Ronaldo's winner as the Portuguese stabbed a shot through his legs from close range.
Real may also leave with regrets, as they could have taken a commanding lead into next week's second leg at the Bernabeu.
As well as Neuer's string of saves, captain Sergio Ramos had a last-minute header correctly ruled out for offside before Ronaldo lost his footing when seemingly well placed to meet a cross when unmarked four yards out.
Ronaldo was anonymous in a first half which was controlled by Bayern but the former Manchester United man dominated after the break to carry his side into a strong position.
The forward had not scored in the Champions League since a draw with Borussia Dortmund in late September but found space brilliantly 97 seconds into the second half to steer Dani Carvajal's precise cross into the bottom corner.
Real have now scored in each of their 53 games in all competitions this season.
Ronaldo was then the key figure as Martinez picked up two bookings in three minutes, both for needless fouls around the halfway line on the Real number seven.
Bayern, who had won their last 16 Champions League games at the Allianz, were forced into survival mode by the red card and only Neuer's excellence prevented the Spanish side from forging ahead.
But Ronaldo found the winner Real deserved when he poked substitute Marco Asensio's cross through Neuer's legs with the studs of his right boot.