The Spaniard has won two league titles in Germany and Bayern are currently six points clear at the top of this season's Bundesliga.
Karl-Heinz Rummenigge, Bayern chief executive, said of Guardiola's decision to leave the club: "We are thankful to Guardiola for all he gave our club and hope to celebrate more success in the current season.
"Carlo is a calm, balanced expert, who knows how to deal with stars and opts for a balanced style of play - we were looking for this, and have found it.
"We are looking forward to working together."
Guardiola, 44, has not revealed where his next destination will be but has said in the past that he is keen to test himself in the Premier League. Close ties to director of football Txiki Begiristain and chief executive Ferran Soriano make Manchester City a strong possibility.
Current City manager Manuel Pellegrini has said this week that he wants Guardiola to replace him at the Etihad Stadium - just not yet.
“Pep Guardiola will work here. I do not know if it will be next season here at City, or even at another club but I am sure that one day he will come to the Premier League.
"I hope that he will have the option to work at Manchester City and I say that because I love this club and I hope, in the future, he can work here also.
"But I do not worry. The only thing I am worried about is this year, trying to win the most amount of trophies I can."