After France winger Franck Ribery gave Bayern an early lead, Real's Mesut Oezil equalised before Germany striker Gomez hit the 90th-minute winner ahead of next Wednesday's return leg at Madrid's Bernabeu stadium.
"We knew Real are capable of scoring lots of goals and it's a pity we conceded one," said Bayern's goal-scorer Ribery. "We'll have some chances in the return leg and we have to make the most of them."
Bayern coach Jupp Heynckes was delighted with his team's performance as they fought until the end.
"All in all, we deserved to win," said the Bayern coach. "Madrid started well, but we took command and played very cleverly. "I had said we would play with passion for 90 minutes and the players did just that. "A great compliment to my team, they ran very hard and played a great game. They gave everything."
But Real coach Jose Mourinho is backing his team to turn things around in the second leg in Madrid.
"We have the second leg to come and it's not like we have to make a historical comeback, what we have to do is very achievable," said the ex-Chelsea boss. "If we win at home, we are there. "I am optimistic we can return here to play the final. "It's the first defeat in this Champions League for us but it's not one that leaves me with negative feelings."
The Bavarians preserved their remarkable record of having never lost at home to Madrid, with nine wins and a draw in their 10 meetings in Munich since 1976.
"It's a pity that we conceded a goal at the end as until then we had defended well, but it's over now," said Real captain Iker Casillas. "We also managed to score which is obviously very important. "We both had chances and we have to score at home in the second leg now, simple as that."
Bayern are now in pole position to return to Munich for this year's Champions League final on May 19, when they will face either holders Barcelona or Chelsea, who meet in the first leg of their semi-final on Wednesday.
Both teams started at full-strength, with wingers Ribery and Arjen Robben either side of Toni Kroos behind Gomez in Bayern's attack.
Mourinho opted to start with France's Karim Benzema as striker in front of Oezil, who was flanked by Argentina's Angel di Maria and Portugal star Cristiano Ronaldo.
After Ribery had seen a penalty appeal turned down, he then fired past Real goalkeeper Iker Casillas from close range on 17 minute after Madrid failed to deal with a Kroos corner and the ball fell kindly to the Frenchman. The goal injected some confidence into the Bayern attack as midfielders Kroos and Luiz Gustavo got the better of their Real opponents, while Bastian Schweinsteiger forced Casillas into a diving save.
Bayern picked up two first-half bookings, with both defender Holger Badstuber and Robben earning cautions, but neither will miss the second leg.
The visitors got back on level terms just seven minutes into the second half.
Bayern goalkeeper Manuel Neuer managed to save Ronaldo's shot, but when Benzema fired the ball back across goal, the Portugal star teed up Oezil for a simple tap-in as Neuer watched on helplessly. Having squandered several chances and had a penalty appeal turned down, Gomez finally found the net in the 90th minute after Bayern captain Philipp Lahm squared for the German star to bundle the ball home from close range.
There was a melee in the dying stages after substitute Thomas Mueller was heavily fouled by Madrid replacement Marcelo, who was shown a yellow card, but Bayern now have everything to play for in the return leg in Spain.