The 26-year-old Argentine international joined Roma from Boca Juniors in 2000.
He will join his new club on July 1. His arrival is a response to criticisms that Real Madrid's squad lacked strength at the back.
Samuel is the second player in the past week to leave the financially-troubled Serie A team following French defender Jonathan Zebina, who signed for Juventus.
La Liga
Roberto Carlos extends Madrid stay
11/06/2004 AT 17:56
Earlier this season Italian sports daily La Gazzetta dello Sport estimated Roma's debts at 250 million euros.
La Liga
Morientes: I will return to Real
11/06/2004 AT 09:50
La Liga
Departing Fernandez says sorry to Betis fans
09/06/2004 AT 12:55