Spectacular first-half strikes by Messi and Antoine Griezmann from long-range appeared to have put Barca in the driving seat with the visitors rarely threatening.
However, Barca keeper Marc-Andre ter Stegen was adjudged to have felled Rafa Mir on the stroke of halftime as the forward attacked a cross. He picked himself up to net the penalty and halve the deficit against the run of play.
Mir then missed a golden chance to pull one back when he contrived to head over from close range moments later, and was made to pay as Messi rounded off the win in the final minute of normal time with a deflected effort.
"I've always said there's all to play for in the league title race," Barca coach Ronaldo Koeman said.
"It's a great result to build on. We were good in the first-half and we kept our intensity up in the second, too.
"We can't afford to drop any more points because we've already dropped quite a lot."
Huesca, meanwhile, remain bottom of the table on 20 points, four from safety.
Messi equalled Xavi Hernandez's all-time appearance record for Barcelona.