The Bundesliga side overcame Jose Mourinho's men at the death after Los Blancos paid the price for trying to cling to a draw ahead of next week's second leg.
The hosts were ecstatic when they opened the scoring through Franck Ribery in the 17th minute but Real Madrid showed great resilience and pulled back a vital away goal through Mesut Ozil early in the second half. However, Bayern assumed pole-position in the tie when Mario Gomez slotted home a 90th-minute winner.
It was Bayern's ninth win in 10 home games against Real Madrid.
The atmosphere inside the stadium was pulsating and the early moments were certainly befitting of the occasion. With the play fairly stretched, it was the visitors who settled quickest and began to control possession.
With seven minutes played, Bastian Schweinsteiger gave the ball away in his own half and Angel Di Maria took the opportunity to slip a pass through to Karim Benzema. However, the Frenchman failed to put his effort to either side of Manuel Neuer, who tipped it over.
Los Blancos' keep-ball tactics were clearly grating on the crowd and they were further incensed when Ribery saw his claim for a penalty turned down in the 15th minute.
Frustration then turned to elation just two minutes later as the Bavarians took the lead. Toni Kroos' corner was not dealt with by Sergio Ramos and the ball fell to Ribery, who rifled a low shot past Iker Casillas from 12 yards.
Bayern remained solid and was getting a lot of joy in the wide areas but Jose Mourinho's men regained their composure to end the half on a brighter note. The hosts could not regain the ball and were being penned back in their own final third.
Madrid's best chance during this spell came as Benzema cut inside of David Alaba to the right of the goal but he could only muster a tame effort straight at the keeper, who held comfortably.
In its haste to find an equalizer, Madrid went two behind after a swift counterattack from the hosts. With five first-half minutes remaining, Gomez burst in on the left-hand side of the area but his shot was kept out by a diving Casillas, who got two strong hands to it and turned the ball behind.
Los Blancos began the next period in a similar vein and drew level in the 53rd minute. The Bayern defense was caught ball-watching as Benzema's mishit shot committed Neuer to the far post. Cristiano Ronaldo managed to reach the effort and cut the ball back into the danger area for Ozil to tap in to a vacant net.
However, that almost paid dividends for Bayern when Kroos whipped in a fantastic free kick from wide on the left after 71 minutes. Ramos again failed clear the danger and was fortunate that Gomez could not keep his instinctive shot below the bar, blazing over from just outside the six-yard box.
The visitors became more conservative as they sought to hold on to the draw but Bayern continued to pile on the pressure and reaped the rewards in the 90th minute.
After missing two headers earlier in the game, Gomez remained positive and was on hand to slide the ball into the back of the net after some superb play on the wing by Philipp Lahm created the space for a tantalising cross.
The result means that Bayern takes a slim advantage into the second leg at the Santiago Bernabeu on April 25 and has every chance of becoming the first side to win the Champions League on its own ground.