The 26-year-old, who was Bundesliga top scorer last season and jointly leads this season's charts with 23 goals, has been in fine form, also netting 11 times in the Champions League.
"I have always said Bayern are a great club, I feel well here and I see the best sporting prospects," Gomez said in a statement.
Bayern, chasing a treble of domestic and European trophies, beat Olympique Marseille 2-0 in their Champions League quarter-final second leg on Tuesday to win 4-0 on aggregate.
Gomez became the most expensive transfer in Bundesliga history when he joined Bayern in 2009 for what is still a record sum of 35 million euros (£30m).
After a quiet first season, Gomez has firmly established himself as the number one striker at Bayern and has also won a regular starting spot for Germany.
"Mario Gomez has shown in the past years that he has become one of the best scorers in Europe," Bayern sports director Christian Nerlinger said. "We are happy that he will keep scoring his goals for us."