Franck Ribery played a one-two with Thomas Muller and the winger ran clear of the Mainz defence to enter the box and slide in a neat left-footed finish past goalkeeper Jannik Huth.
But Brazilian centre-back Andre Ramalho quickly struck back for Mainz, beating Manuel Neuer with a stunning effort from just outside the penalty area.
Bayern retook the lead in the 11th minute, Martinez powering in a header from a right-wing Joshua Kimmich corner, and Carlo Ancelotti's men held out to win the 45-minute clash and claim the club's third Telekom Cup title.
"It's always nice to win," said goalscorer Ribery. "But it's also important that we played well."
Bayern return to Bundesliga action on Friday when they travel to Freiburg.