Leipzig head coach Nagelsmann is to become the new Bayern boss and has agreed a five-year contract beginning on July 1, both clubs confirmed on Tuesday.
Bayern, who are on the verge of a ninth successive Bundesliga title, have agreed to terminate the contract of Hansi Flick, who informed the club he did not want to stay in charge beyond this season.
Nagelsmann has Leipzig on the brink of sealing second place in the Bundesliga, which would be their best finish.
There also remains a chance he will guide the club to their first major silverware with Bayern already out of the DFB-Pokal ahead of Leipzig's semi-final against Werder Bremen on Friday.
The 33-year-old finished third in the top flight and reached the Champions League semi-finals with Leipzig last season – his first after joining from Hoffenheim - but revealed the Bayern job was too difficult to turn down.
"I will leave RB Leipzig with a heavy heart," Nagelsmann told Leipzig's website.
"I’ve been able to coach a special team here at a club with huge opportunities and the best possible conditions to work in.
"Our run to the Champions League semi-finals was definitely the highlight of our time together. It was a moment that I'll never forget.
"We're currently enjoying the best Bundesliga campaign in the club's history too. We want to make sure it ends up our best finish and then also lift a major trophy for the first time as well.
"It's too early to say my goodbyes and speak about my memories because I'm not done here yet in Leipzig.
"My mission may end here in the summer, but I will give my absolute all until then. We've grown into a real tight-knit bunch that finally wants to win a title.
"Leipzig are a special club – nevertheless, I am leaving. I made no secret of the fact that the head coach position at FC Bayern appealed to me and that I would like to take on this job if the opportunity ever arose.
"The position at FC Bayern is a unique opportunity for me. I would therefore like to thank Oliver Mintzlaff and the rest of the board at RB Leipzig for finding a solution with FC Bayern and making this possible for me.
"Now we're shifting our full attention to the remaining three Bundesliga games and of course the DFB-Pokal semi-final, to try and finish the season as successfully as possible."
Talk emerged on Monday that Bayern and Nagelsmann had quickly agreed terms but that a fee to release him from his RB Leipzig contract was proving a sticking point.
According to widespread reports, an agreement has been reached between the clubs that would make Nagelsmann the world's most expensive head coach.
The Athletic indicated Bayern will pay around €20million, while further reports suggest add-ons could take the total closer to €25m.
Nagelsmann, 33, had also been linked with Tottenham after their decision to sack Jose Mourinho last week.
However, as a boyhood Bayern fan from the Bavarian town of Landsberg am Lech, the Allianz Arena has long been touted as his dream destination.
When Nagelsmann does finish at Leipzig, he will do so with a sense of pride.
He added: "Since my first game in August 2019 against VfL Osnabruck, RB Leipzig, the whole region and all the staff and players here have meant so much to me.
"We've experienced so many special things during our eventful time together.
"Everybody here, including the staff who work alongside the team, have ensured that we've written plenty of stories for the club's history books and are continuing to do so now. I'm really proud of that."