The Catalan giants will look to break the record that Real Sociedad set in 1979-80 when they host Valencia next weekend, with Ernesto Valverde's side now just seven matches from becoming the first La Liga side to go through a campaign without tasting defeat.
Messi was the star of the show at the Camp Nou with a magnificent treble, as Barca stretched their lead at the top to 12 points ahead of second-placed Atletico Madrid's derby against Real Madrid on Sunday.
Coach Valverde named a strong side despite having a Champions League quarter-final, second leg on Tuesday at Roma, with Messi, Luis Suarez and Philippe Coutinho all starting.
Suarez passed up two good early chances to put Barca ahead, prodding wide and then seeing visiting goalkeeper Ivan Cuellar deny him with an excellent save.
But the hosts moved in front in the 27th minute through a trademark Messi free-kick. The Argentinian stepped up and curled the ball over the ball and into the bottom corner from 25 yards out to score his 27th league goal of the season. It didn't take the 30-year-old long to double his tally for the day, latching onto Coutinho's through ball and firing low into the corner.
Barcelona eased off in the second half, perhaps with the midweek clash at the Stadio Olimpico on their minds, and Leganes hit back midway through the second half. Former Liverpool winger Nabil El Zhar cut inside and fired into the bottom corner from the edge of the box despite slipping as he shot.
But the home side finished off the match as a contest late on with Messi completing his third hat-trick of the season in style. The five-time Ballon d'Or winner chested down Ousmane Dembele's pass and dinked the ball over Cuellar with consummate ease.
Barca will now turn their attentions to avoiding a disaster in Italy as they defend a 4-1 first-leg lead, still bidding to complete the treble with a Copa del Rey final against Sevilla to come in a fortnight's time.
Earlier on Saturday, Sevilla's hopes of qualifying for Europe next season took a hit with a 4-0 thumping at Celta Vigo. Vincenzo Montella's side were taken apart by former striker Iago Aspas' hat-trick, as the ex-Liverpool man strengthened his claim for a place in Spain's World Cup squad.
Sevilla face Bayern Munich on Wednesday's at the Allianz Arena needing to overturn a 2-1 deficit to reach the Champions League semi-finals.
Real Betis moved up to fifth with a fourth straight win as they saw off Eibar 2-0, while Alaves all but secured safety by beating Getafe.