They laboured for much of the first half before falling behind at the start of the second, but Lionel Messi struck twice and Thiago got a goal to give them the points.
And Kaka scored two penalties, with Christiano Ronaldo adding a third, as Real Madrid won in the Basque country to defeat Athletic Bilbao and keep alive their title hopes ahead of the Clasico next week.