Silva, 31, left Manchester City at the end of last season, bringing an end to a trophy-laden nine-year spell at the Etihad Stadium.
The Portuguese was heavily linked with a return to Spain, with Barcelona and Atletico Madrid also reportedly chasing his signature before Real emerged as the clear frontrunners.
He becomes Real's second free transfer signing of the summer, following defender Marc Cucurella's £52m move from Chelsea.
Real did not win a trophy last season, finishing eight points behind La Liga champions Barcelona and exiting the Champions League at the quarter-final stage.
Silva leaves City as one of the most decorated players in the club's history, having won 19 major honours across his nine years in Manchester.
He made 460 appearances for City, scoring 76 goals and providing 77 assists, and took on the captaincy last season following the departure of Kyle Walker.
Silva guided Mnachester City to Carabao Cup success against Arsenal and FA Cup glory over Chelsea in his final campaign at the club, closing out his spell on a high despite City's wider struggles.
Real Madrid confirmed the move with a statement outlining the length of the deal.
"Real Madrid CF and Bernardo Silva have reached an agreement whereby he will be a Real Madrid player for the next two seasons, until June 30, 2028."
Silva is currently with Portugal at the World Cup and is expected to play a significant role for his country as they continue their tournament campaign.
He featured in both of Portugal's warm-up friendlies in June before departing for the finals, and is expected to link up with his new Real Madrid team-mates for pre-season training once the World Cup concludes.
Real Madrid are understood to be targeting Inter Milan's Denzel Dumfries, while France defender Ibrahima Konate is set to join after his Liverpool contract expires.
Antonio Rudiger has this week signed a contract extension with Real until 2027, adding further stability to Mourinho's backline as he rebuilds the squad following a trophyless campaign.
