Thiago Alcantara struck twice on Thursday (AEDT) and Robert Lewandowski, Arjen Robben and substitute Thomas Mueller were also on target as the Bavarians, struggling for form recently, inflicted Arsenal's first defeat of the season in Europe's premier club competition.
Alexis Sanchez had equalised for the visitors, scoring on the rebound after Manuel Neuer saved his 30th-minute penalty.
The sides meet for the second leg in London on March 8.
Casermiro wonder goal puts Real in control