The 32-year-old had a medical in Munich on Thursday as transfer negotiations were completed. He was to give a press conference in Madrid on Friday.
"Real Madrid CF and Bayern Munich have reached an agreement for the transfer of Xabi Alonso," said a brief statement released by the Spanish giants.
A member of the Spanish side that won the World Cup in 2010 and the European Championships in 2008 and 2012, Alonso joined Real from Liverpool in 2009.
Alonso announced his retirement from international football on Wednesday. But the recent purchase of German midfielder Toni Kroos had also complicated his future with Real, despite his role in the club's Champions League triumph last season.
Bayern's Spanish coach Pep Guardiola has midfield troubles with injury worries over Javi Martinez, Bastian Schweinsteiger, Rafinha and Thiago Alcantara.