The 31-year-old last week revealed he wanted to leave the Bernabeu after four years and Rossoneri vice-president Adriano Galliani moved quickly to persuade the 2007 Ballon d'Or winner to return to Serie A in a deal ending on June 30, 2015.
Galliani sealed the deal to bring Kaka back to Milan, who he left to join Real in a reported 70million euros (£56million) move in 2009, between 2.30am and 3.00am on Monday morning on what is understood to be a free transfer.
A statement by Milan read: "Adriano Galliani sealed the deal to bring Kaka back to Milan between 2.30 and 3.00 this morning.
"The player has been given a contract ending on 30 June 2015."
Kaka was an increasingly isolated figure during the three-year tenure of Jose Mourinho and Sunday's signing of Wales winger Gareth Bale from Tottenham for a world-record 100million euros fee would have pushed the Brazilian further down the pecking order.
Galliani and Kaka landed in Milan just before lunchtime and the Brazil striker told the Milan Channel on his arrival: "Four years have passed and now I'm back.
"It's been two weeks that I thought if I could come back, a return to Milan would be ideal for me.
"In the last few days, I've been dreaming of hearing the fans sing my name in the San Siro again."
After leaving Sao Paolo as a teenager in 2003, Kaka won the Champions League and one Scudetto during his six years in Milan - also landing the prestigious Ballon d'Or award in 2007 - and lifted the Primera Division trophy, the Copa del Rey and the Spanish Supercup with Real.
On linking up with Italy forward and new team-mate Mario Balotelli, who made the move to Milan from Manchester City earlier this year, Kaka is confident they will team up well.
He added: "Mario is a great player and I want to do well with him. I hope I can do with him what I used to do with (Filippo) Pippo Inzaghi.
"I have a lot of desire to play and it's a very important year for me due to the World Cup, but right now I just want to do well for Milan, my team.
"I was very sad to leave this team and now it's great to be back. I'm very happy."
Galliani was delighted with the deal and believes Kaka will attract some long-lost fans back to the San Siro.
He told acmilan.com: "It was around 2.30 last night when the deal was done.
"Now I hope Kaka's orphans, the 12,000 or 15,000 fans that stopped coming to the stadium when he left, will come back now that Kaka's returned.
"He really made a huge effort to come back and has shown real affection for the jersey."