Barca should have killed the game off in the first half as Luis Suarez and Messi missed two big chances each against La Liga's bottom side.
However, Suarez rounded off a fine team move to break the deadlock just before the hour before Messi's double in the final 17 minutes moved him ahead of Cristiano Ronaldo as La Liga's top scorer with 11 for the season.
"We are Barcelona and we know we can never give up," Suarez told BeIN Sports Spain.
"We are candidates to win the league, it doesn't matter if we are four, five, six or seven points behind, we won't give up."
Also Read
Barca were in desperate need of a lift after conceding in the last minute to Real in last weekend's El Clasico.
The Catalans' upturn in performance since captain Andres Iniesta's return from a six-week injury layoff continued in the first half at Osasuna, but Barca were in danger of paying for their profligacy in front of goal.
Osasuna remain five points off safety and it was easy to see why they are still without a home win all season as the hosts were opened up at will.
Suarez turned provider for Messi, but Nauzet Perez rushed from his goal to block.
And the goalkeeper frustrated the five-time World Player of the Year again moments later by just raising a hand in time to divert Messi's attempted chip behind.
"We were patient and generated a lot of chances," said Barca boss Luis Enrique.
"The goals came in the second-half, but we deserved to go in front in the first-half and have a much easier game."
Sergio Leon gave Barca a scare at the start of the second-half when his brilliant lob over Marc-Andre ter Stegen came back off the underside of the bar.
Barca created fewer chances in the second half, but were more clinical.
The second goal came about in similar fashion 13 minutes
later as Alba again got to the by-line and his cross was volleyed home by Messi at the near post.
Valencia remain above the relegation zone only on goal difference despite more penalty heroics from goalkeeper Diego Alves in a 3-2 defeat at Real Sociedad.
Alves saved the 23rd penalty of 47 he has faced in Spanish football from Carlos Vela, but it mattered little as a Willian Jose double and Juanmi's injury time strike moved Sociedad up to fourth.
Valencia have won just one game in eight since former Italy boss Cesare Prandelli took charge in October.
Two goals in two minutes from Joselu just after the hour mark looked set to end Real's unbeaten streak stretching back to April after Alvaro Morata's opener.
However, even without the rested Cristiano Ronaldo, Real maintained their six-point lead at the top of La Liga as Mariano Diaz equalised six minutes from time before Ramos's late intervention.