The title-holding Bavarians saw their record 10-match Champions League winning streak broken - after having led the game two goals up.
It was also their first home defeat - after 18 wins in Munich - since losing 2-0 at home to Arsenal in last season's knock-out stages of the Champions League in March.
"Perhaps we needed a defeat to remind us of a few things," said Guardiola, whose Bayern side are on a 40-match unbeaten run in the Bundesliga.
"We have made a good start in the league, in the cup, in the Champions League, winning in Manchester, perhaps we thought things were too easy.
"Congratulations to City, I also congratulated my players in the changing room for finishing top of their group.
"The most important thing now is that we learn the lessons from the defeat."
With both teams already through to the knock-out stages, City avenged October's 3-1 defeat to Bayern at the Etihad Stadium, but still finished second to the Germans in Group D with both teams on 15 points.
Remarkably, City pulled off a famous European victory at the defending champions without starting their first-choice side.
With a home game against Premier League leaders Arsenal on Saturday, City coach Manuel Pellegrini began with first-choice strike pairing Sergio Aguero and Alvaro Negredo, plus captain Vincent Kompany, on the bench.
"It's an important victory for the club, not just for me," said City coach Manuel Pellegrini, who later admitted on television that he was not aware a 4-2 win would have put City through as group winners.
"I have been trying to find a style of play here, some of our results had not been that good in the Premier League and this is very good for the club.
"The first message for the squad is that they can win without the likes of Kompany, Aguero and Negredo.
"I told the players this was the chance to prove to me what they can do, that is the most important thing for me."
It had started ominously for City as Bayern raced into a 2-0 lead after 11 minutes.
Goals by Thomas Müller and Mario Götze left Bayern cruising before David Silva pulled one back for City as it finished 2-1 at the break.
Captain Aleksandar Kolarov then converted a penalty to pull level on 59 minutes before James Milner gave City a deserved lead three minutes later with a superb curling shot to complete the miraculous fight-back.
City had needed to win at least 3-0 to wrestle top spot from Bayern and their hopes seemed to have evaporated in the opening minutes.
The first goal came when centre-back Dante's superb cross-field pass from inside the Bayern half found Müller in behind the defence to drill his shot past Hart with five minutes gone.
With Bayern dominating possession and attacking in waves, the second followed on 11 minutes when Götze fired home from close range after City fail to clear their lines from a Toni Kroos' corner.
The 21-year-old midfielder profited after Mario Mankiewicz's blocked shot fell into his path and it looked like it was game over for City.
Pellegrini responded with a change with Pablo Zabaleta on for Micah Richards.
The game's tempo dropped sharply after the second goal and City profited when Fernandinho fired wide before Silva pulled a goal back on 28 minutes after some good work from Milner and Jesus Navas.
City drew level after the break when Milner was pushed from behind by Dante following a strong run into the area and Neuer dived the wrong way as Kolarov fired home.
Bayern now had a fight on their hands and Milner put City ahead three minutes later when he curled his shot around Neuer and inside the far post to the delight of the travelling City fans in the sold-out 68,000 crowd.
This was only the second time Bayern have thrown away a two-goal lead in the Champions League having gone down 3-2 to AS Roma in November 2010.