The Argentine now has 371 goals for Barca in all matches including friendlies, surpassing the record of Paulino Alcantara, who played for the club between 1912 and 1927 and notched up 369 goals.
Messi struck the opener after 18 minutes in a morale-boosting victory for the Catalan side ahead of the crunch game at the Bernabeu.
Barca knocked Manchester City out of the Champions League last Wednesday but went into the Osasuna clash on the back of two defeats from their past three league games.
Alexis Sanchez pounced to double the lead after 22 minutes and Andres Iniesta hit a fine left-foot drive from 20 yards with little over half an hour gone.
Chances continued to come and go before Messi bagged another with a shot into the roof of the net from close range just after the hour before substitute Cristian Tello netted with a powerful strike.
Messi was given time in the area to score his third late on, his 18th of the season, and he then set up Pedro to complete the rout in stoppage time.
"As long as the people want me here I will stay here. My intention is to retire with the Barcelona shirt," Messi told reporters.
Looking ahead to the Clasico, he added: "Hopefully, we can play a good game. We know it will be difficult but we will do our best to fight for the league until the end."
Coach Gerardo Martino was cautious looking ahead to the Real match.
"It helps to play well before going to the Bernabeu but we mustn't get carried away," he told a news conference.
"It isn't easy to score seven goals but we can't go from being one moment a disaster to the other fantastic."
Brazilian Neymar was a surprise absentee from the starting lineup in a win that left Barca on 66 points from 28 matches, four behind Real and one behind Atletico Madrid who beat Espanyol 1-0 on Saturday.
There were some anxious fans in the crowd after their recent poor run but Messi calmed the nerves with the opening goal from a well-worked passing move. Xavi found Sanchez who rolled the ball across the area and Messi steered home.
Chilean Sanchez continued his good form with his 17th goal of the season, slotting a left-wing cross from Jordi Alba.
Barca were stroking the ball around with confidence when Iniesta found his range with a shot from outside the area which found the corner.
Messi combined with Iniesta to get another midway through the second half before Tello, who came on a minute earlier, cracked in a 25-yard effort. With Osasuna in disarray, Messi and Pedro hit two more.
Real Sociedad boosted their Champions League hopes as Imanol Agirretxe gave them a 1-0 win over Valencia which put them in fifth on 46 points.
It was their first win in three attempts and Valencia had more of the ball but they were missing in-form striker Paco Alcacer through suspension and struggled to create chances.
Jonas did shoot wide for the visitors before the break but Agirretxe flicked a header into the top corner on the hour mark and Antoine Griezmann wasted the chance to give them a more comfortable win as he was denied by Diego Alves when through on goal.
Elsewhere, Sevilla beat Valladolid 4-1 while Elche and Real Betis drew 0-0.
- Sports & Recreation
- Andres Iniesta