The Argentina international had not scored in his last three appearances - his worst run for a year - since his equaliser in the San Siro late last month but netted from the penalty spot after Ignazio Abate had fouled Neymar in the area.
Sergio Busquets exploited a non-existent Milan defence to head in Xavi's free-kick five minutes before the interval but Barca's control was shaken by Gerard Pique's own goal with virtually the last kick of the half.
However, Messi made sure of the victory seven minutes from time when he converted a return pass from Cesc Fabregas for his 65th goal in 82 Champions League appearances.
In Group G, Atletico Madrid qualified for the knockout stage for the first time since the 2008-09 season - also becoming the first side to be confirmed as group winners - with a comprehensive 4-0 win at home to Austria Vienna, setting a new club record with a fourth consecutive European win.
The Spanish side continued to match defending champions Bayern Munich as the only teams with 100 per cent records as Miranda, Raul Garcia and Filipe Luis gave them a 3-0 lead with Diego Costa adding the fourth eight minutes from time.
Former Porto favourite Hulk scored against his old club and missed a penalty as second-placed Zenit St Petersburg remained on course to progress after a 1-1 draw.
The Brazilian equalised for the Russian club after Lucho Gonzalez had given the visitors the lead midway through the first half but he missed from the spot, costing his second-placed side the chance to open a three-point cushion over their rivals.
In Group F, Italian club Napoli beat Marseille 3-2.
Striker Gonzalo Higuain scored twice, both times giving his side the lead with his second proving the decisive strike.
Andre Ayew put the visitors in front but Gokhan Inler and Higuain scored within three minutes of each other to give Napoli a 2-1 half-time lead.
Florian Thauvin pulled Marseille level just past the hour only for Higuain's intervention with 15 minutes remaining, meaning a draw in Dortmund next time out will secure qualification.
In Group E, Basel snatched a 1-1 draw at home to Steaua Bucharest thanks to Giovanni Sio's 90th-minute goal after Federico Piovaccari had put the visitors ahead in the first half.