Osasuna 2 - 3 Barcelona
Los Cules had trailed 2-1 going into the final 10 minutes of the game after Miguel Flano and Walter Pandiani's second-half strikes overhauled Samuel Eto'o's 44th-minute opener.
But Xavi's low drive and Messi's spectacular shot from the edge of the area gave Barca two goals in five minutes and a sixth consecutive win in La Liga.
The match was played in freezing conditions and it was not the classic that the scoreline would suggest.
Osasuna mostly chased their illustrious visitors and were fortunate to only be one goal down at half-time.
Thierry Henry went close when two of his shots were saved by back-up keeper Roberto after marauding runs on the left, while Messi hit the woodwork with a low, angled finish after a mazy dribble.
Barca finally went in front when great play on the left by Messi set up Eto'o, who drilled low into the bottom left from the edge of the box.
Home boss Jose Antonio Camacho brought on Josetxo for the whole of the second half and Javier Portillo just before the hour mark.
The changes seemed to work as on 62 minutes Miguel Flano - who at 5'9 is no giant for a defender - leapt well to arc a looping header over Victor Valdes from a right-wing corner.
Four minutes later Messi was denied by Roberto, who saved well from the Argentine after he initially spilled a long-range shot from Eto'o.
There was a moment of controversy when Osasuna played on and almost scored after Messi went down, although replays later suggested that he feigned injury and referee Jose Antonio Teixeira Vitienes had already instructed them to continue playing.
Osasuna took an unlikely lead when Jaroslav Plasil found Juanfran on the right and his low cross was prodded past Valdes by arch-poacher Pandiani for his third goal in as many games since recovering from injury.
Barcelona were frustrated and emotions almost spilled over a second time when the referee halted a Dani Alves counter-attack following Plasil's collision with Valdes, fearing a head injury to the Czech midfielder.
Pep Guardiola's men managed to challenge their aggression effectively, though.
Alves went on another run down the right and drilled a low ball to the unmarked Xavi, who fired past the helpless Roberto to equalise with nine minutes left on the clock.
And on 85 minutes Messi unleashed his winner from the edge of the box after he was allowed to cut in from the right on to his preferred left foot.