Prior to Lille’s Bafode Diakite, Jonathan David, and Jonathan Bamba goals, PSG held a 2-0 lead but trailed 3-2.
In the 87th minute, Kylian Mbappe, who had already scored once, added his second goal to give PSG a chance to win.
The match was then dramatically won by Messi with a 22-yard free kick.
Neymar, a Brazilian forward who scored in the first half and was kicked in the calf before falling awkwardly on his right ankle five minutes into the second half, will be PSG’s biggest concern despite the victory.
While he was being carried away at the Parc des Princes, he had his hands over his face and appeared to be in considerable pain.
PSG now lead Ligue 1 by eight points, but second-placed Marseille might close the gap to five points if they win at mid-table Toulouse later on Sunday (19:45 GMT).
PSG entered this game on a three-game losing streak after suffering defeats to Marseille 2-1 in the French Cup quarterfinals, Monaco 3-1 in Ligue 1, and Bayern Munich 1-0 at home in the first leg of their Champions League last-16 encounter.
Yet, they appeared to have all control against Lille and led 2-0 after just 17 minutes.
The first goal was scored by Mbappe, who displayed excellent talent to jink past Tiago Djalo and Diakite. Neymar then extended the lead by scoring off a rebound after Vitinha’s shot hit Lucas Chevalier in the goal.
Yet Diakite’s looping header in the 24th minute gave Lille, who had been defeated 7-1 by PSG at home in August, a chance to respond.
Before Neymar was injured, PSG had already lost left-back Nuno Mendes to illness.
Following a foul by Marco Verratti, David’s penalty made it 2-2, and Bamba’s stout goal gave the visitors the lead in the 69th.
Lille tried to hold on, but they couldn’t. With three minutes remaining in regulation, Mbappe scored his 15th Ligue 1 goal of the year with a low effort into the corner, and Messi’s injury-time victory added one more dramatic moment to this seven-goal thriller.
The next game for PSG is against title challenger Marseille away from home on February 26.