Hansi Flick had fallen out with Bayern's sporting director Hasan Salihamidzic over transfers
Julian Nagelsmann will take over as Bayern Munich coach from next season to replace Hansi Flick, the reigning German champions announced on Tuesday.
Nagelsmann, who is just 33, will move from RB Leipzig on a five-year contract after Flick, whose relationship with the club's powerful sporting director Hasan Salihamidzic has deteriorated over transfer policy, asked to be released from his contract.
"I am convinced that Bayern's sporting future will be a very successful one with Julian Nagelsmann," Oliver Kahn, the club's former goalkeeper who will succeed Karl-Heinz Rummenigge as Bayern chairman next year, told the club's website.
Nagelsmann has never hidden his desire to coach Bayern, especially as he comes from the Bavaria region.
Last season, he led Leipzig to the semi-finals of the Champions League as well as third place in the Bundesliga, and this season his side has replaced Borussia Dortmund as Bayern's main title challengers.
With three games of the season remaining, Leipzig are the only team who can still prevent Bayern from winning a ninth consecutive Bundesliga title, although they sit seven points off the pace.
Bayern will get another chance to wrap up the title for a record-extending 30th time in the Bundesliga era when they host Borussia Moenchengladbach next weekend.
Flick took over as Bayern coach in late 2019, succeeding the sacked Niko Kovac.
He won the Bundesliga and German Cup last season and added the Champions League, as Bayern beat Paris Saint-Germain behind closed doors in the final in Lisbon to become European champions for the sixth time.
Bayern added the UEFA and German Super Cups at the start of this campaign and then won the Club World Cup in Qatar at the start of this year.
However, as reports of a rift between the former Germany assistant coach and Salihamidzic emerged, Bayern were knocked out of the Champions League in the quarter-finals by PSG earlier this month.
"FC Bayern agreed to current head coach Hansi Flick's request to have his contract terminated on 30 June 2021, two years earlier than its original expiry date," the club said.
Flick said: "The last two years will be unforgettable for me. The emotions, the wins, the titles, but also the day-to-day work on the pitch have been a lot of fun for me, it's been an amazing time."
Nagelsmann spent time on the books at Bayern's neighbours 1860 Munich, before his playing career was cut short by a knee injury at the age of just 20.
He moved into coaching and became the youngest permanent head coach in Bundesliga history when appointed by Hoffenheim in February 2016 at the age of 28.
He steered the small club from south-western Germany to safety in that season before successive top-four finishes in the next two campaigns.
His star has continued to rise at Leipzig where a young side has established itself as a leading force in the Bundesliga and in Europe -- they again reached the Champions League knockout stage this season, losing to Liverpool in the last 16.
"Julian Nagelsmann represents a new generation of coaches. Despite his young age, he already has an impressive CV. We are convinced that with Julian Nagelsmann, we can continue the magnificent successes of recent years," Bayern president Herbert Hainer said.
One of the stars of his Leipzig team has been French international defender Dayot Upamecano, the 22-year-old centre-back who will also join Bayern next season.
That transfer was announced in February. Upamecano, who had a 43 million-euro ($51.9m) buyout clause in his contract, will also sign a five-year deal at the Allianz Arena.