The win takes some pressure off coach Pep Guardiola, who was accused of sticking to his trademark possession game against Real despite his team failing to score over both legs in their 5-0 aggregate defeat.
"At this club you always have to win," Guardiola told reporters. "Winning, winning, week in, week out.
"We worked our way into the game and it was good for our rhythm ahead of the German Cup final (on May 17)."
Guardiola stuck with his game plan again on Saturday but it took a long time for the Bundesliga champions to get started, needing more than half an hour to carve out their first chance against the battling hosts.
That was, however, all Goetze needed to score after he played a quick one-two with Arjen Robben, charged into the box and slotted in for the lead in the 32nd minute.
The Germany international, who was not in the starting lineup in their Champions League defeat to the Spaniards and has had a largely substitute's role under coach Pep Guardiola, played a part in the second goal early in the second half with Thomas Mueller getting the last touch.
Goetze, expected to be among the key Germany players at the World Cup finals in Brazil, struck again in the 70th minute, rifling in from a Mueller header.
Hakan Calhanoglu pulled one back for the hosts with a fine low drive from 18 metres in the 72nd but substitute Claudio Pizarro quickly restored order with a bicycle kick three minutes later after keeper Rene Adler had saved his first effort.
Bayern were left with ten men when defender Jerome Boateng was sent off for slapping Hamburg's Kemel Demirbay.
The result leaves Hamburg anchored in the relegation playoff spot with one game left.
"The second goal was the turning point," Hamburg coach Mirko Slomka told reporters. "But still there were a lot of good things to see. The necessary desire and passion are there."
The former European champions, who are the only team never to have played in the second division since the creation of the Bundesliga in 1963, have 27 points in 16th place with Nuremberg, who lost 2-0 to Hanover 96, on 26.
Eintracht Braunschweig lost 1-0 at Augsburg thanks to a stoppage time chip by Raul Bobadilla to remain in last place with 25 points.
Schalke 04 took a big step towards securing automatic qualification to the Champions League, easing past Freiburg 2-0 with goals from defender Kaan Ayhan and forward Klaas-Jan Huntelaar to move up to 61 points, three ahead of Bayer Leverkusen in fourth.
Borussia Dortmund, who have long secured a second spot finish, edged past Hoffenheim 3-2 but with Dortmund's Robert Lewandowski, moving to rivals Bayern next season, failing to score in his last home game despite the crowd urnging him on.
VfB Stuttgart conceded a stoppage time Ivica Olic goal to lose 2-1 to VfL Wolfsburg but still secured another year in the top division, staying five points away from the playoff spot.
Wolfsburg moved to within a point of Leverkusen and can now challenge for a Champions League qualifying round spot and a fourth place finish.
Borussia Moenchengladbach, wearing their new home kit reminiscent of their striped shirt of the 1970s, made sure they would be playing in Europe next season with a 3-1 win over rivals Mainz 05. Gladbach moved up to sixth place on 55, with Mainz in seventh on 50.
- Sports & Recreation
- Pep Guardiola