The Italy international netted once in each half as the Serie A giants overcame their La Liga counterparts in an entertaining encounter.
Both sides named strong starting XIs for the clash, with Real coach Carlo Ancelotti coming up against his former employers.
Jeremy Menez opened the scoring for Milan, taking advantage of a poor error from Real defender Nacho to coolly convert.
El Shaarawy produced a fine finish to add to that lead, before Ballon d'Or frontrunner Cristiano Ronaldo claimed his almost obligatory goal to pull one back for Ancelotti's men.
The La Liga leaders created several chances in the second half, but Milan looked equally dangerous and further goals from El Shaaraway and Giampaolo Pazzini made for a convincing victory, despite Karim Benzema's late penalty.
Real came into the game on the back of a 23-match winning streak in competitive matches, and Ancelotti will expect an improved showing when they return to Liga action at Valencia on Sunday.
After a low-key opening to proceedings, Milan capitalised on Nacho's mistake to take the lead midway through the first half.
The defender's ball back to goalkeeper Keylor Navas was woefully short and Menez took control, left Navas on his backside with a feint and tucked the ball into the bottom left-hand corner.
That advantage was doubled shortly after the hour-mark when El Shaarawy received the ball on the edge of the penalty area, cut inside on his right foot and drove a low finish into the same corner of the goal.
Milan's double strike appeared to spark Real into life, and they halved the deficit 10 minutes before half-time through Ronaldo.
Jese Rodriguez drove forward from midfield and released the Portuguese, who took advantage of poor positioning from former Real keeper Diego Lopez to slot home at the near post.
Real made a raft of changes at the break and one of those, keeper Iker Casillas, was called into action almost immediately as he kept out El Shaarawy's effort with his legs.
El Shaarawy was not to be denied for long, however, as he swept home from close range after being teed up by substitute Andrea Poli's pass.
Once again Real's response was positive, and almost resulted in a goal when Jese's deflected shot struck the base of the post and rebounded to safety.
With 17 minutes to go, Pazzini headed in M'Baye Niang's pinpoint cross, before Niang's foul on Nacho allowed Benzema to convert from the spot in the 84th minute.
It was too little, too late for the European champions, however, as Filippo Inzaghi's men held on for the victory.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.