Bayern had said on Thursday it was close to inking the deal for the former Spanish international.
The 32-year-old Alonso will help fill the void left by the injury to fellow Spaniard Javi Martinez.
Alonso joined Madrid from Liverpool in 2009 and helped it win the Champions League last May. Madrid eliminated title holder Bayern in the semifinals.
Alonso is the second important player to leave the European champions this week after Angel Di Maria signed with Manchester United.