Magellan and his party were hired by the Rajah of Cebu to fight Lapu-Lapu, chief of the Mactan tribe in 1521. During the battle on Mactan island on April 27, Magellan was killed in the heat of battle. The Mactan won the battle.
So, in other words, he got involved in a local conflict and was killed by an enemy in war. He was an enemy of the Mactan and an ally of Cebu, and the Mactan ended up killing him.