Barca have now gone 38 top-flight matches without defeat, levelling the previous best mark by Real Sociedad which has stood since 1980.
Messi made it six straight LaLiga games with a goal by netting a wonderful free-kick shortly before the half-hour mark, and then swiftly got his second of the night after being set up by Philippe Coutinho.
Nabil El Zhar got a goal back for Leganes after the break as Barca's performance dropped off significantly, but the hosts made sure of victory when the magnificent Messi grabbed his third three minutes from time.
Barca are now 12 points clear of nearest challengers Atletico Madrid at the top of the table ahead of Diego Simeone's side facing city rivals Real Madrid in Sunday's derby, as title glory moves closer for the Catalans with only seven games to go.
Ernesto Valverde's men now turn their attention to Tuesday's Champions League quarter-final tie against Roma – where they hold a 4-1 lead – as their pursuit of a treble continues, while 14th-placed Leganes are left to reflect on a 12th consecutive league away game without a victory.
In the first chance of note, Gerard Pique poorly headed over from 12 yards after being set up by Coutinho's cross from the left, the former Liverpool star in as one of four changes from Valverde.
Coutinho soon drilled an effort which was saved by Ivan Cuellar at his near post, while Luis Suarez showed great technique in sending a first-time volley just wide from Sergi Roberto's high pass.
Cuellar then sprawled to somehow keep out a Suarez shot with his legs after Messi laid it on a plate for him six yards out, as the Leganes defence waited for an offside flag that did not arrive.
After his team-mate's big miss, Messi took over the show.
First, having been hacked down by Dimitrios Siovas 25 yards from goal, Messi got up and took the resulting 27th-minute free-kick himself, finding the corner from with a magnificent whipped effort that left Cuellar with no chance.
Five minutes later, he was at it again. Coutinho raced infield and delivered an inch-perfect pass which the Argentina international took in his stride while holding off two defenders before sending a left-footed strike into the bottom corner from 15 yards.
Leganes almost threw a spanner in the works early in the second half when El Zhar's cross from the right met by Claudio Beauvue's powerful header, which only just cleared Marc-Andre ter Stegen's crossbar.
The visitors threatened again when an unmarked El Zhar headed off target from 12 yards after being found by Darko Brasanac, as Barca appeared to take their foot off the gas.
And El Zhar made up for that miss 22 minutes from time, picking his spot in finding the bottom corner from 18 yards, with the aid of a slight deflection off Sergi Roberto, after being set up by Gabriel Pires.
Messi's attempted chip was saved by Cuellar right at the edge of his penalty area as Barca sought to re-assert their dominance late on, and he was not to be denied his hat-trick.
Just as it looked like Barca would have to settle for a one-goal win, Messi was found by Ousmane Dembele at the end of a probing passing move and flicked a looped finish over Cuellar from inside the six-yard box, ensuring a crowd that was becoming restless ultimately went home happy.
6 - Lionel Messi is the first player to score six direct free-kick goals in a single La Liga season since Ronaldinho Gaucho in 2006/07 (6). Brilliance. pic.twitter.com/MWkEfcSpJf— OptaJose (@OptaJose) April 7, 2018
Key Opta facts:
- Lionel Messi is the first player to score six direct free-kick goals in a single La Liga season since Ronaldinho in 2006-07.
- The Argentina striker is the first player to score from outside the box in six consecutive LaLiga games since at least 2003-04 (six goals).
- Messi has scored seven direct free-kick goals this season in all competitions.
- Philippe Coutinho has made two assists in his two LaLiga games, as many as he had in his previous 25 in the competition.
- Andre Gomes completed 73 of his 77 passes against Leganes, his highest tally in a LaLiga game.