After France winger Franck Ribery gave Bayern an early lead, Real's Mesut Özil equalised before Bayern striker Gomez sealed the deal just in time.
Bayern coach Jupp Heynckes was delighted with his team's performance, as “Madrid started well, but we took command and played very cleverly.”
"All in all, we deserved to win," he added. "I had said we would play with passion for 90 minutes and the players did just that.”
But Real coach Jose Mourinho is backing his team to turn things around in the second leg in Madrid and said he felt confident that if his team won at home next week, they too could have the Allianz area in sight.
"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, adding that although it was the first defeat for Real in this Champions League, it had not left him feeling negative.
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. They are now in pole position to return to Munich for the final on May 19, when they will face either cup-holders Barcelona, or Chelsea.
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 Özil, who was flanked by Argentina's Angel di Maria and Portugal star Cristiano Ronaldo.
After Ribery’s penalty appeal was rejected, he fired past Real goalkeeper Iker Casillas from close range on the 17 minute, after Madrid failed to deal with a Kroos corner.
The goal gave Bayern a confidence boost and as midfielders Kroos and Luiz Gustavo pushed forward, while Bastian Schweinsteiger forced Casillas into a diving save.
But the visitors got back on level terms just seven minutes into the second half as
when Benzema fired the ball back across goal sunk Real’s first and only goal.
Then, having squandered several chances and had a penalty appeal turned down, Gomez finally found the net in the ninetieth minute after Bayern captain Philipp Lahm squared for him to bundle the ball home from close range.
There was a melee in the dying stages after substitute Thomas Müller 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.
The match was preceded by drama in the changing rooms, when a few Real players’ boots were stolen. In total, the thief managed to snatch six pairs of boots and some shirts from Real's dressing room in the stadium.
Spanish media reported that Germany’s Özil may have been one of the targets and that Ronaldo definitely had three pairs stolen. A complaint has been lodged by Real to European football's governing body UEFA.