Ronaldo, the Champions League’s record goalscorer, scored twice in the second half to end an 11-hour wait for a goal and put the 11-times winners in charge.
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.
Most Goals in UEFA Competitions
Vital miss from Vidal?
Without injured striker Robert Lewandowski, who has scored 38 goals in 40 appearances this season, Bayern were reliant on midfielder Vidal to carry a goal threat as they edged a scrappy first half.
The Chilean powered in a header to open the scoring and was on the end of Arjen Robben’s clever cross to head their next best chance over the top.
But perhaps the key moment of the night came right at the end of the half. Referee Nicola Rizzoli awarded a penalty when Franck Ribery’s shot hit Carvajal at the top of his arm, but after a lengthy wait Vidal fired it well over the top.
Carlo Ancelotti’s side barely registered after the break, managing just two attempts on goal in contrast to their 11 in the first half, and they must hope that Poland goal-machine Lewandowski can recover from a shoulder problem to lead the line in Madrid next week.
Man of the match – Manuel Neuer (Bayern Munich)
Ronaldo’s double turned the game on its head but without Neuer’s show of defiance Real would already be assured of a place in their 28th European Cup semi-final.
In the first half he touched a Benzema header onto the underside of the crossbar before, in a matter of minutes, turning Bale’s header over from just four yards out, saving from Benzema with his legs and then making a superb one-handed save to keep out Ronaldo’s drive from eight yards out.
Four superb saves – but then he was beaten by Ronaldo’s second from close range.
“It’s a big result, it isn’t easy to come here and win.
“It always comes down to small margins, you have to believe until the end and we did that. When they went down to 10 men we created plenty of chances and it was a shame that we didn’t get the third. Their keeper was in top form.
“A third goal would have been a bonus.”
Zinedine Zidane, Real Madrid Manager
“It will be difficult in 90 minutes in Madrid but we are still alive. We missed the penalty which would have put us 2-0 up and then within three minutes it was 1-1.” Carlo Ancelotti, Bayern Munich Boss