La Liga: Lionel Messi hits 4, Cristiano Ronaldo passes 300 goals
Messi cleverly waited for Osasuna goalkeeper Andres Fernandez to go to ground before walking the ball into the net.
The visitors, who are third from bottom on 18 points, equalised in the 24th minute through midfielder Raoul Loe but their chances of causing an upset were ended when Alejandro Arribas was shown a second yellow card three minutes later.
Defender Arribas handled in the penalty area and Messi hammered the ball home from the spot before Daniel Alves unselfishly set up Pedro to tap in Barca's third goal five minutes before the break.
Messi, who also headed against the post in the first half, completed his hat-trick in the 56th minute and grabbed a fourth goal two minutes later.
"The important thing was to get through the match because when so much is being said about the 'Clasico' (against Real) it's not easy," Messi told reporters.
"We came out the same way as always and we achieved our goal."
Real, who host holders Barca in the King's Cup semi-final first leg on Wednesday, put a turbulent week behind them with Portuguese Ronaldo again on song.
The buildup to the game was dominated by a report in Marca sports daily that said captains Iker Casillas and Sergio Ramos had threatened to leave unless coach Jose Mourinho was dismissed.
Real president Florentino Perez called a news conference to deny the story, accusing the newspaper of lying, and the Spanish champions let their football do the talking on Sunday with a comfortable victory against their city rivals.
Mourinho, who turned 50 on Saturday, left several key players out of his starting lineup including Xabi Alonso and Sami Khedira while goalkeeper Casillas was missing after breaking a bone in his hand in midweek.
Barca's interim coach Jordi Roura, standing in for Tito Vilanova who is recovering after throat surgery, also rested Andres Iniesta and Cesc Fabregas, the latter only coming on for Xavi late in the game.