Real, eyeing a third straight title, needed a 44th-minute equaliser by Marcelo against the run of play to cancel out Joshua Kimmich's opener after Bayern, chasing three trophies this season, had missed a hatful of chances.
A perfect break allowed Real substitute Marco Asensio to beat Sven Ulreich with a fine effort and grab the winner in the 57th minute.
The result was identical to last year's quarter-final first leg between the teams before Real won the return game as well on the way to the first successful Champions League title defence.
Liverpool crushed AS Roma 5-2 in the other semifinal first leg.