Milan shock Real at the Bernabeu
The Brazilian condemned Real to their first defeat in the competition this season when he arrived late at the back post to smash Clarence Seedorf's looping cross past a helpless Iker Casillas, two minutes from time.
Real had taken the lead through Raul after a dreadful error from Milan keeper Dida in the 19th minute, his strike equalling Gerd Mueller's record of 66 goals in the three main European club competitions - the European Cup/Champions League, Cup Winners Cup and UEFA Cup/Fairs Cup.
The hosts were comfortably in control until a stunning 30-metre shot from Andrea Pirlo beat Casillas at his near post in the 62nd minute, and the Spain keeper gifted Pato Milan's second four minutes later.
Real substitute Royston Drenthe crashed in an equaliser after 76 minutes before Pato struck again.
Milan and Real have six points from three matches, with Olympique Marseille and FC Zurich on three points.
"We lost our shape after the 2-1 and Milan made the most of it," Real coach Manuel Pellegrini said.
Milan's attacking line up failed to trouble the hosts early on as they were starved of the ball with Real, missing the injured Cristiano Ronaldo, dominating possession.
Real were unlucky not to win a penalty after 14 minutes when Gianluca Zambrotta brought down Karim Benzema without getting the ball, but openings were proving hard to find.
Milan held firm until Dida gifted Raul the opening goal, when he spilled the ball in the area under no pressure after collecting Esteban Granero's tame shot.
Brazilian midfielders Kaka and Ronaldinho had hogged the pre-match headlines but they had a quiet evening.
Real's Kaka was a peripheral figure against his former club, while Milan's Ronaldinho was whistled and jeered every time he touched the ball for the five-year spell he spent with Real's arch-rivals Barcelona.
The match burst into life unexpectedly when Pirlo stunned everyone with a shot from distance that flew past Casillas.
Suddenly Milan had their tails up and Massimo Ambrosini's long pass through for Pato provoked an error from Casillas.
The home keeper raced out of his area, misjudged the ball and Pato scampered past him to score into the empty net.
Real drew level when Raul's corner was pulled back to an unmarked Drenthe on the edge of the area, and the Dutchman drilled a low shot just inside the post.
The game opened up and Milan thought they had grabbed a winner when Thiago Silva powered in a header, only for the referee to rule it out unfairly for a foul in the area, before Pato hit the decider.
"We played an incredible second half. We were much better, we were never vulnerable at the back, we created chances and turned the game to 2-1," said Milan coach Leonardo.
"Even though they got back to 2-2 I never thought we would lose. The important thing for me today was the team spirit I saw out on the pitch there today."