Real said on Sunday that the 25-year-old would be presented in the next few days after passing a medical with the club.
"He was the signing our coach (Fabio Capello) had asked for because he considered him to be essential and so we are delighted that we are able to grant him his wish," said Real Madrid president Ramon Calderon.
Neither club disclosed fee details but Spanish media say the nine-times European champions have agreed to pay Lyon some 26 million euros ($33.37 million), making Diarra the most expensive close-season signing in the Primera Liga.
Lyon were originally reported to have asked for over 35 million euros for the Mali international but Calderon said they had settled for much less.
Diarra is the fourth player to join Real since Calderon was elected president last month and brings the club's total close-season spending to over 60 million euros
Real have also signed Italy's World Cup-winning
A key figure in the Lyon midfield, Diarra joined the French side from Vitesse Arnhem in 2002 and has won the last four Ligue 1 titles with the club.
He will team up with Emerson to form a tight partnership in the centre of midfield, a problem area for Real since French midfielder Claude Makelele left for Chelsea in 2003.
Real, who have not won a major trophy since then, have said that their spending has not yet finished. They have expressed an interest in Arsenal forward Jose Antonio Reyes but are also looking for another centre back.
"We can still sign more players and I think we will do so before the transfer deadline expires," Calderon said on Friday.