Franck Ribery gave Bayern the lead in the 17th minute on Tuesday night, but Germany midfielder Mesut Ozil levelled early in the second half.
Bayern dominated for large parts of the game and found a deserved winner in the last minute through the prolific Gomez, giving them a slender advantage to take to Spain next week.
Madrid had the better of the first 10 minutes, but all they could produce was a shot from Karim Benzema which Manuel Neuer tipped over the crossbar after Bastian Schweinsteiger uncharacteristically gave away possession in midfield.
Bayern responded and took the game to their visitors.
Franck Ribery felt he should have had a penalty when he went to ground under a challenge from Sergio Ramos inside the area, but referee Howard Webb waved play on.
But the France winger was soon celebrating as he rifled Bayern in front with a shot from 10 yards, which only just avoided Luiz Gustavo in an offside position.
The hosts were looking dangerous every time they came forward, and those attacks were becoming increasingly frequent, with Arjen Robben marauding down the right and Ribery causing confusion in the Madrid defence down the Bayern left.
But they failed to make their pressure count with Gomez's shot turned behind by Iker Casillas just before the break.
And Madrid levelled on the counter-attack eight minutes after the break.
Cristiano Ronaldo looked to have wasted the chance when his tame effort, one-on-one with the keeper, was saved by Neuer.
But Karim Benzema picked up the rebound and took the ball back into the area, playing it across goal to Ronaldo, whose clever pull-back gave Ozil a tap-in from a yard out.
Madrid enjoyed their best spell of the game with Bayern and many of the 66,000 fans in the Allianz Arena becoming increasingly anxious.
The Bavarians regrouped and soon got back on top of the game, though.
Gomez spurned an excellent chance to put them back in front in the 71st minute when he lifted the ball over the bar from close range after being teed up unwittingly by Sergio Ramos.
Gomez headed straight at Casillas as the hosts continued to be frustrated in their attempts to find another goal.
Two hopeful penalty claims were waved away by referee Webb as Bayern kept on pressing and their pressure finally paid off in the 90th minute.
Philipp Lahm got to the byline down the right and fired a ball in which Gomez, sliding in, bundled over the line at the near post.
Bayern Munich are kings of Germany and hope to be crowned kings of Europe at Wembley later this month!
The push for more color on their cleats, adidas has added the infrared colorway to the adiZero f50 Line.
The adidas Nitrocharge is the most talked about new cleat in years. It was released on May 15 and will take your pitch by storm.
Chelsea won the Europa League on Wednesday. The Blues defeated Benfica 2-1 on injury time goal from Ivanovic.
adidas has added some color to the Predator line with this new colorway. Released on May 17. Be the first to own a pair.
Ajax has released their away soccer jersey for the 2013/14 season. Get in the Gear.
The Juventus jersey for the 2013/14 was released earlier this week. Be the first to wear the colors of the Old Lady.
The Liverpool 2013/14 soccer jersey is available for pre-order. The Reds are eager to compete for silver next season.
One of the top soccer cleats for control the Nike CTR360 will not let you down on the field in tight spaces.
The new PUMA evoSPEED 1.2 FG. A great new soccer cleat with classic look and first time for PUMA to be on the cleat.