The 33-year-old Nagelsmann will move to Bayern on July 1 and has agreed a deal running through until June 30, 2026.
Flick is to leave Bayern at the end of the current season after he requested his contract be terminated, two years earlier than its original expiry date of 30 June 2023.
"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 in a statement.
Nagelsmann became the youngest coach in Bundesliga history when he took over at Hoffenheim at the age of 28 in February 2016.
His burgeoning reputation saw him take the reins at RB Leipzig in 2019, leading them to the UEFA Champions League semifinals in his first season in charge.
Nagelsmann said the opportunity to take over at Bayern was a unique one for him.
"Leipzig are a special club -- nevertheless, I am leaving. I made no secret of the fact that the head coach position at FC Bayern München 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," he said in a statement issued by RB Leipzig.
The young coach will have big boots to fill after Flick guided Bayern to a stunning six trophies in the 2019-20 season -- Bundesliga, German Cup, German Super Cup, UEFA Champions League, UEFA Super Cup and FIFA Club World Cup.
The 56-year-old is likely to mark his farewell with a record-extending ninth consecutive league title.
The Bavarian side sit seven points clear of second-placed RB Leipzig with just three games remaining in the current Bundesliga season.
Flick has been strongly tipped to succeed Germany national team coach Joachim Löw, who will be stepping down from the position after this summer's European Championship.