A statement on the Real Madrid website confirmed the two clubs had agreed a fee and Heinze will sign a four-year contract with the Spanish champions.
The Argentine had his heart set on a move to Liverpool and took his case to a Premier League tribunal after United refused to sell him to their rivals.
But after a League panel ruled against Heinze, the 29-year-old agreed to join Real for a reported £8m fee.
The move brings to an end Heinze's three-season stay at Old Trafford.
Heinze will undergo a medical in Madrid before completing the move which will see him replace veteran Brazilian Roberto Carlos, who left the Bernabeu in the summer.