Goals from Marcelo and Marco Asensio either side of half-time cancelled out Joshua Kimmich's 28th minute opener during the first leg played in Munich to leave the double defending European champions in the driving seat ahead of Tuesday's second leg in Madrid.
Leganes managed to pull one back, which inadvertently contributed to a chaotic end to the match.
Leganes had to wait until the 36th minute for their first chance as Amrabat struck the crossbar with an effort from outside the box.
Zidane's side had their second on the stroke of the interval when Mayoral turned a Mateo Kovacic cross home from close range.
Real Madrid will shift focus to LaLiga matters when they host Leganes at the Santiago Bernabeu this evening. Let's not forget that the victor of the Bayern Munich-Real Madrid clash will play against the victor of the Liverpool-Roma.
Leganes had Brazilian midfielder Gabriel dismissed after the final whistle as he showed dissent when referee Ignacio Iglesias blew for full-time just as Leganes were lining up a shot from 25 yards.