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.
Support the USA national team with an official 2013 Centennial soccer jersey from Nike. Its Official.
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.
England will debut their new away jersey against Brazil at the Maracana Stadium on Sunday. Get in the Gear
Barcelona will wear this new home soccer jersey next season. They also have a new away jersey. Get them both.
Manchester City will try and win back the Premier League title with this new soccer jersey from Nike!
The hoop soccer jersey is classic and cool. The USA hooped jersey is perfect for every fan. Get in the Gear.
The USA will wear their blue away jersey against Jamaica in their World Cup qualifier on Friday night.
The Nike Hypervenom is the latest cleat silo from Nike. It will change your game and is worn by the game's top players.
Germany is one of the top teams ever and they will play the US on Sunday.
Jamaica is in need of a win against the U.S. Get the jersey of the Reggae Boyz.