Kroos helped Bayern win the Champions League in 2013 and domestic doubles the past two seasons.
He was on Germany's 2010 World Cup squad, but was given a bigger role in coach Joachim Loew's team during qualifying for this year's tournament in Brazil.
"We would like to thank Toni Kroos for his time here in Munich," said Bayern chairman Karl-Heinz Rummenigge. We enjoyed great success here together. We would like to wish him and his family all the best in Madrid and with Real."