Bayern will host Barca at the Allianz Arena on July 24, a little over three weeks after Guardiola is due to take over from Jupp Heynckes as Bayern coach.
The deal between the two clubs was finalised during the general assembly of the European Club Association in Doha.
The match will be named the Uli Hoeness Cup after the Bayern president, with all proceeds donated to charity.
"I am really looking forward to this game," Hoeness said on the Bayern website.
"I thank the board for this gift on my 60th birthday. I am particularly pleased about the fact that I can use the proceeds to support social causes."
Guardiola starred as a player and coach for Barcelona.
He was part of the side that won the European Cup in 1992 while also collecting six La Liga titles, two Copa del Reys, four Supercopas, the UEFA Cup Winners' Cup and two UEFA Super Cups.
As coach, he helped build a side widely regarded as the best in the world, winning two Champions League crowns, three straight La Liga titles, two Copa del Reys, three Supercopas, two UEFA Super Cups and two FIFA Club World Cup titles between 2008 and 2012.
Guardiola stepped down as Barca coach at the end of last season to take a sabbatical, before last month announcing he would take over at Bayern once Heynckes retires in the summer.