Why was Martin Luther excommunicated?

Martin was excommunicated because he was trying to go against most of the teachings of the Catholic church. (Which was caused by the 95 Theses, which Luther posted in Germany), The Church finally excommunicated him in 1521, and Luther was later banished in 1522.

On 15 June 1520, the Pope warned Luther that he risked excommunication unless he recanted 41 heretical sentences drawn from his writings, including the 95 Theses, within 60 days. Luther set fire to the Popes warning at Wittenberg on 10 December 1520,

Martin Luther was excommunicated by Pope Leo X on 3 January 1521 for heresy and apostasy, and for leading others astray.