Massimilano Allegri's men were pushed all the way by the hosts but two great strikes from El Shaarawy and the Frenchman ensured their progression to the next round
The hosts started the game impressively but were unable to make the most of their superior possession due to a lack of creativity in the side.
They were punished early in the second half when Stephan El Shaarawy put the Rossoneri ahead, and after Bram Nuytinck was shown a red card, Philippe Mexes netted one of the goals the tournament so far to secure the points. Tom De Sutter did pull one back for the Belgian outfit but Alexandre Pato restored the two-goal cushion in stoppage time.
Signs from the start of the match suggested an upset could well be on the cards though, as with 13 minutes gone Milan Jovanovic was released by Dieumerci Mbokani's through-ball. The former Liverpool man had time to pick his spot from the left of the area but his shot lacked pace and was directed straight at Christian Abiatti.
Despite its dominance, it was not until the 34th minute that Anderlecht came close again. Lucas Biglia's in-swinging free kick was almost turned in at the back post by Mbokani but Kevin Constant got in between to hook the ball to safety.
Milan put an abject first half display in the rear-view, finding the net two minutes after the restart. Mattia De Sciglio's deep cross was controlled exquisitely by El Shaarawy and with his second touch he curled a superbly-placed shot past Silvio Proto.
The hosts were regularly hitting a brick wall as they looked to find a route to goal, and any hopes of a comeback were extinguished in the space of one minute.
Nuytinck was dismissed in the 70th minute as he took away Pato's legs with the Brazilian ready to go through on goal, but the real hammer blow came from the resultant free kick.
With his back to goal, Mexes controlled the delivery on his chest and improvised magnificently to loop an absolutely stunning overhead kick past an idle Proto from the top of the box.
John van den Brom brought on De Sutter in the 75th minute as his side had nothing to lose and the change proved beneficial as the striker poked in from close range moments later.
However, as the Paars-wit continued to press they left themselves exposed at the back and Milan took full advantage as El Shaarawy burst past the defense before sliding the ball across to Pato for a simple finish in stoppage time.
The win ensures Milan will compete in the last 16 while Anderlecht can only hope for a third-place finish and a spot in the Europa League.