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 return fixture
The hosts opened the scoring through Franck Ribery in the 17th minute but Los Blancos 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.
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 the bar.
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 were 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 pinned 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 could only muster a tame effort straight at Neuer, who held comfortably.
In their haste to find an equaliser, Madrid almost went two behind after a swift counterattack from the hosts. 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 second half in a similar vein to the first and drew level in the 53rd minute. The Bayern defence was caught ball-watching as Benzema's mishit committed Neuer to the far post. Cristiano Ronaldo just reached the ball before it went out of play and cut it back into the danger area, leaving Ozil with a simple tap-in.
Mourinho's men were beginning to pose a great threat on the counterattack as they limited the hosts to a number of speculative crosses into the box.
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 to 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, who created the space for a tantalising cross.
The result means that Bayern take a slim advantage into the second leg at the Santiago Bernabeu on April 25, while Madrid slide to their first Champions League loss of the season.