The 26-year-old Brazil international has signed a five-year deal and will join City on their pre-season tour of the United States.
Danilo can play in both full-back roles as well as in midfield, and is City's fourth major signing of the summer.
"There was interest from other clubs, but it has always been my ambition to play for Pep Guardiola," he said.
His arrival takes City's summer spending to almost £150m, following the recruitment of England international Kyle Walker (£45m), Portugal midfielder Bernardo Silva (£43m) and Brazilian goalkeeper Ederson Moraes (£35m).
The club have also agreed a £52m deal for 23-year-old Monaco defender Benjamin Mendy and sold left-back Aleksandar Kolarov to Serie A club Roma for £4.5m.
Danilo's move is subject to him receiving a work permit, meaning he is unlikely to make his debut when City face Real on Wednesday in Los Angeles.
The former Santos player joined Real in 2015 from Porto but started just 17 La Liga games last season.
In two years at the Bernabeu, he won La Liga, two Champions Leagues, the Uefa Super Cup and the Fifa Club World Cup.
"Danilo is a fine player who offers great versatility," said City director of football Txiki Begiristain.
"He can operate in several different roles in both defence and midfield, increasing Pep's options ahead of the new season."
Manchester City have completed the signing of Real Madrid defender Danilo for a fee believed to be £26.5m.