Kaka smiling again after scoring
Cristiano Ronaldo set up the Brazilian playmaker in the 82nd minute for a first-time strike high into the net, his first goal this season in only his third appearance after coming back from a knee operation.
"This is what I needed, to return to play here at the Bernabeu, to score a goal and for the team to win the game. It's a happy night for me," Kaka told reporters.
"It was a difficult game and they played very well, but we managed to equalise before the break after a first half in which we struggled. In the second half we got the goals we needed."
Ronaldo twice dragged Real back into the game with equalisers against third-placed Villarreal, before firing them in front in the 79th minute.
Kaka, who had played the last 15 minutes away to Getafe in the league and Levante in the King's Cup, was thrown on in the 70th minute and wrapped up the match to a huge roar from a packed Bernabeu.
The 2007 World Player of the Year had surgery on his left knee at the start of August and has spent the last five months working on his recovery.
The goal will help win over doubters as he has struggled to live up to his 65 million euro price tag since switching from AC Milan in 2009.
With striker Gonzalo Higuain sidelined due to a back problem and Karim Benzema inconsistent at best, Kaka provides coach Jose Mourinho with welcome options going forward as they take up their pursuit of leaders Barcelona into the new year.
Sunday's win left them two points short of the champions, with one match to play before the mid-point of the season.