A fatal mistake from goalkeeper Andre Onana gave Bayern the lead when they failed to save a weak shot from Leroy Sane in the 28th minute.
Jamal Musiala sprinted down the left flank and passed a ball to Serge Gnabry to score and extend the home side's superiority.
Rasmus Hoylund revived United's hopes at the start of the second half but that lasted only four minutes.
The referee awarded a penalty to Bayern for a hand touch on Christian Eriksen after returning to VAR and Harry Kane scored in the 54th minute.
Casemiro reduced the deficit a minute before the end to set the stage for a thrilling finish.
But substitute Matthijs Thiel relieved the nerves of Bayern fans with a fourth goal in the second minute of stoppage time.
Casemiro added United's third goal with a header after Bruno Fernandes' free-kick.