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 got scored 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.
Ajax's 1-0 win over Celtic courtesy of Lasse Schone's goal just after the interval closed the gap to second-placed Milan in Group H to one point.
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.
Last season's runners-up Borussia Dortmund's first home defeat to an English side thanks to Arsenal midfielder Aaron Ramsey's solitary goal allowed the Gunners to assume top spot in Group F.
However, they are ahead of Napoli only on goal difference after the Italian club won 3-2 in Marseille to make it a three-way contest for qualification.
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.
Chelsea need a point from their match in Basle on match day five to secure their progress after a comfortable 3-0 win over Schalke.
Basle snatched a point 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.
- Sports & Recreation
- Gerard Pique
- Sergio Busquets
- Ignazio Abate
- Cesc Fabregas