Pep to take over at Bayern in summer
Spaniard Guardiola, 41, will take over from Heynckes, who has decided not to extend his contract, in July and has agreed a deal until the end of June 2016.
"We are very pleased that we have managed to convince the football expert Pep Guardiola, who was coveted and contacted by many top clubs, to come to Bayern Munich," Bayern CEO Karl-Heinz Rummenigge said on Wednesday.
He said Heynckes had decided to retire at the end of the season and Bayern were replacing him with the best coach on the market.
FEATURE Inside the mind of Pep Guardiola
"Pep Guardiola is one of the world's most successful coaches and we are sure that he can bring a great deal of sparkle not only to Bayern Munich but to German football in general," he added.
Bayern's announcement ended weeks of speculation about which club would win the race to hire Guardiola, who announced this month he would return to coaching next season after taking a year out to rest.
He stepped down as Barca coach at the end of last term after leading the La Liga club, where he began his career as a player, to 14 trophies in four years, including two Champions League crowns and three Spanish league titles in a row.
It is a significant coup for the German club, runners-up in Europe's elite club competition last season, given that big-spending rivals, including Chelsea, Manchester City and Paris St Germain, were reportedly also hoping to hire Guardiola.
Bayern were also losing Champions League finalists in 2010 and their last European Cup win was in 2001.
It is equally a boost for the booming Bundesliga which this season has seen three of its clubs top their groups and move into the Champions League knockout stages.
"We can only congratulate Bayern for signing Pep Guardiola," said German football league (DFL) managing director Andreas Rettig.
"This shows the international standing of the Bundesliga and with Guardiola there will be even more interest in the Bundesliga."
Speaking to the English Football Association as part of the organisation's 150th anniversary celebrations this week, Guardiola had said his ambition was to manage in the Premier League.
Under his guidance, his first top-flight coaching job, Barca won plaudits for their entertaining, attacking brand of football based on domination of possession and swift one-touch passing.
Many credit Johan Cruyff, another former Barca player and coach, with creating the club's distinctive style and Guardiola played under the Dutchman in the 1990s when they won their first European Cup.
The ex-Spain international also won praise for getting the best out of quadruple World Player of the Year Lionel Messi, to whom he gave more liberty to roam the pitch than his predecessor as coach Frank Rijkaard.
Whether Guardiola's philosophy and tactics will suit Bayern and the Bundesliga remains to be seen.
There is also a question mark over whether he can manage a group of players the majority of whom, unlike at Barca, did not come through the club's academy.
One player at the German club he knows well is Spain midfielder Javi Martinez, who joined in the close season for a Bundesliga record of 40 million euros.
Guardiola may be hoping to lure some of his former charges to Germany to join Martinez and his imminent return to coaching has already prompted Barca president Sandro Rosell to warn him off.
But even if he does not attract former