The two-time defending champions were second best for much of the first half after Bayern took the lead through Joshua Kimmich, but the hosts could not build on that advantage despite creating a number of clear chances.
Neither side were anywhere near their brilliant best on the night, but Marcelo's equaliser against the run of play just before half time ensured that Madrid went into the break all square, and Marco Asensio came off the bench to add a second away goal and leave the Spanish giants in the driving seat.
Madrid are now well on course to have the chance to become the first team to win the competition three years in a row since Bayern themselves achieved the feat in the mid-1970s, with the German giants now needing to score at least two goals in the return leg at the Bernabeu next week.
Real Madrid boss Zinedine Zidane left both Gareth Bale and Karim Benzema out of his starting lineup for the match, but was able to welcome captain Sergio Ramos back after he had missed the second leg of the quarter-final through suspension.
Bayern, meanwhile, saw David Alaba fail a late fitness test before kickoff, and their injury issues grew just eight minutes into the match when Arjen Robben was forced off, being replaced by Thiago Alcantara.
It was a surprisingly slow start to the match considering the quality of the two teams on show, and it took until the 19th minute for the first shot on target as Rafinha's long-range effort was comfortably held by Keylor Navas.
Real Madrid eventually began to grow into the game and had their first chance midway through the first half when the ball broke kindly for Dani Carvajal inside the area, but his first-time effort was also straight at the keeper.
It was a game that needed a goal at that stage, and Bayern provided it shortly before half time when Kimmich burst on to a through-ball from Real Madrid loanee James Rodriguez before beating Navas at the near post after the Madrid keeper had gambled and looked to cut out a possible cross into the box.
The mistake will raise more question marks over Navas following previous errors in the quarter-final, and it looked as though it could be the gateway to further problems when the goal appeared to have sparked Bayern into life, even after Jerome Boateng joined Robben in limping off in the first half.
The hosts should have doubled their advantage in the 34th minute when Isco lost possession in a dangerous area to set up Franck Ribery for a one-on-one, but the Frenchman's first touch let him down and allowed Navas to collect it.
Suddenly Bayern were very much on top, and they squandered another clear chance shortly afterwards when Robert Lewandowski flicked a corner on for Mats Hummels, who could not readjust his body in time to keep his volley down.
The hosts were immediately back on the front foot, but Thomas Muller's volley was blocked before it could trouble Navas and Bayern were made to rue their string of missed opportunities just a minute later when Real Madrid levelled the scores against the run of play.
Carvajal's header from the right flank was allowed to travel all the way across the edge of the area, and Marcelo reacted quickest to the loose ball to drill a crisp half-volley into the bottom corner, continuing his record of scoring in all of the knockout stages of this season's competition.
It was an away goal which Madrid didn't necessarily deserve, but Bayern wasted no time in looking to regain their advantage ahead of half time and should have gone back ahead in the first minute of stoppage time when Lewandowski directed his towering header too close to Navas.
Even then there was still time for Bayern to threaten once again, but Muller could not react in time at the back post to turn a flick-on home with the final kick of the first half.
It was a similar story at the start of the second half too as a crucial touch from Raphael Varane denied the hosts what looked like a certain goal, but Jupp Heynckes's side would ultimately pay for their profligacy as Madrid took the lead shortly before the hour mark.
Zidane's unhappiness with his side's first-half display saw him introduce Asensio at half time, and it proved to be an inspired decision as the Spaniard scored just 12 minutes after the break, pouncing on a wayward pass from Rafinha before playing a one-two with Lucas Vazquez and clipping his finish past Sven Ulreich.
Bayern again almost responded quickly when Ribery - who was one of only a handful of Bayern players to live up to their reputations on the night - did well to create space for himself inside the box only to be thwarted by Navas.
Navas came out on top in that duel again just four minutes later as he kept out Ribery once more to go further towards atoning for his earlier error, although it proved to be Rafinha's mistake which was the most damaging on the night.
Bayern's misfiring attackers were every bit as much to blame as Rafinha, though, and their wastefulness was perhaps summed up when Muller and Lewandowski - scorers of 54 goals between them this season - got in each other's way and somehow failed to turn the ball into an empty net from just a couple of yards out.
At the other end, Cristiano Ronaldo was uncharacteristically quiet as his 12-game scoring streak came to an end, failing to find the back of the net for the first time in this season's competition and also bringing an end to his all-time record of scoring in 11 consecutive Champions League games.
The 33-year-old - who scored five of Madrid's six goals when they knocked Bayern out in the quarter-finals last season - did have the ball in the back of the net 19 minutes from time, but his skilful control and finish was correctly ruled out for a handball.
Benzema could have put the tie to bed five minutes later when he broke in behind the Bayern defence and got a shot away from inside the area, but the angle was against him and Ulreich reacted well to deny the Frenchman.
A tiring Bayern side began to fade in the closing stages, but there was still one final chance for the hosts as Corentin Tolisso's pass released Lewandowski inside the area, only for the Pole to fluff his lines with only the keeper to beat and sent his shot comfortably wide when he would have expected to score.
It was a somewhat fitting way to finish an unusual match in which both teams played some sloppy and shambolic football at times, although Madrid will be much the happier of the two sides having recorded a sixth consecutive win over Bayern and their 150th in the Champions League overall - the first team to reach that milestone.
Bayern, meanwhile, fall to their first home defeat of the season in any competition and are now staring down the barrel of a fourth consecutive Champions League semi-final loss - a fate which would also end their hopes of a treble this term.
However, the German champions may take a glimmer of hope into next Tuesday's second leg at the Bernabeu following Juventus's rousing - but ultimately inadequate - 3-1 victory at the stadium in the quarter-finals.
BAYERN MUNICH (4-1-4-1): Ulreich; Kimmich, Boateng (Sule 34'), Hummels, Rafinha; Martinez (Tolisso 75'); Robben (Thiago 8'), Muller, Rodriguez, Ribery; Lewandowski
REAL MADRID (4-1-4-1): Navas; Carvajal (Benzema 67'), Varane, Ramos, Marcelo; Casemiro (Kovacic 83'); Vazquez, Modric, Kroos, Isco (Asensio 46'); Ronaldo