The Brazilian Football Confederation (CBF) announced the Italian, one of the most successful managers in the history of European club football, will take up the position from May 26. Madrid's league season ends on May 25.
Madrid have so far made no comment and reports said they were waiting until the La Liga title is totally out of reach, which could be as early as Wednesday, before announcing a coaching change.
The appointment had been widely anticipated but confirmation increases the likelihood of Real appointing Xabi Alonso before the Club World Cup starts on June 14.
Former Madrid midfielder Alonso has already announced he is leaving Bayer Leverkusen, who he led to the Bundesliga title last season.
Ancelotti will become the first foreigner to lead Brazil and he will take charge ahead of the five-time winners World Cup qualifiers against Ecuador and Paraguay in June.
A statement from the CBF described Ancelotti as "a legend of the game as the most successful manager in football history".
It added: "This landmark moment sees the coming together of two icons - the only five-time FIFA World Cup champions and a coach with an unmatched record across Europe's elite competitions.
Ancelotti has won the Champions League a record five times as a manager, three times across two spells with Real and twice with AC Milan.
He has also won domestic titles with both of those clubs as well as with Chelsea, Paris St Germain and Bayern Munich.
with agencies