Messi was hauled to the ground in the first half by a diving Unai Bustinza wrapping two arms around his waist and he was knocked over again in the second, this time earning a penalty, which he converted.
After 33 appearances, Messi now has 26 goals this season. In his last eight games, he has seven goals and eight assists.
The 17-year-old Ansu Fati had earlier given Barcelona the lead, continuing his breakthrough season with a fifth goal in eight league starts.
Leganes could have made the reigning champions feel uncomfortable if Miguel-Angel Guerrero had taken either of two golden early opportunities but the first was cleared off the line and the second denied by the post.
“We had a bit of luck at the beginning,” admitted Barca coach Quique Setien.
But his team’s reward for a rather underwhelming performance is a five-point advantage over Madrid, who can reduce that back to two by winning at home to Valencia on Thursday.
Valencia will be a more thorough examination of Madrid’s sharpness after the three-month break and Barca can expect tougher tests to come than Leganes too, starting with a trip to third-placed Sevilla on Friday.
Setien will hope Gerard Pique recovers from a nasty challenge that means he will need stitches in his shin. “It split his shinpad,” said Setien. “We hope it’s not serious.”
Leganes have dropped to the bottom of the table and look doomed to relegation, their chances of survival damaged by Barcelona snatching away their top scorer Martin Braithwaite as an emergency signing in February.
Leganes were not alone in feeling the rule had hurt them disproportionately and their sense of injustice might have been deepened by the fact Braithwaite was left on the bench for the entire 90 minutes.
– Wasteful Guerrero –
Barcelona controlled possession in the first half but created little and would have been behind early on if Guerrero had been more precise.
First, Javier Eraso sprinted clear and despite taking too long to shoot, the ball spilled out for Guerrero, whose drive hit the foot of Clement Lenglet on the line.
Guerrero had another go moments later, this time from an acute angle, but his rasping half-volley flew across the area and shaved the far post.
Barca were laboured, even as they dominated the ball, but Leganes might have been tiring by the 42nd minute when Junior Firpo’s bustling run allowed Fati to take an instinctive shot, which he fired through the legs of Rodri Tarin and into the bottom corner.
In the 69th minute, Messi gave his team the cushion they wanted, earning a dubious penalty with a storming run. He won the ball on the halfway line, nutmegged one defender and held off two more.
After an exchange with Luis Suarez, he was finally knocked over, even if it appeared less of a foul than the previous attempts to stop him. Messi curled into the bottom corner.
Suarez had come on for Fati and Sergio Busquets, Gerard Pique, Arthur Melo all departed too as Setien turned his attention to Sevilla. Leganes coach Javier Aguirre was sent off for whistling on the sidelines in injury-time.
– Champions League-chasing Getafe slip up –
Earlier, Getafe dropped more points in the race for Champions League qualification as they failed to break down Espanyol despite their opponents playing most of the match with 10 men.
A goalless draw represents a missed opportunity for Jose Bordalas’ side and they could even have lost to Espanyol, who had Bernardo Espinosa sent off in the 16th minute but are building momentum.
Getafe stay fifth, level on points with fourth-placed Real Sociedad, who will play their game in hand at Alaves on Thursday. Getafe could also be overtaken if Atletico Madrid win on Wednesday against Osasuna.
Espanyol move to within two points of safety after resuming with a 2-0 victory over Alaves on Saturday.
Villarreal stayed in the hunt for the European places by beating Real Mallorca 1-0. Carlos Bacca’s early finish leaves Villarreal seventh, three points from fourth. Mallorca remain 18th.