1) Because Allah knew that the boy would grow up to be evil and cause mischief, and so Allah had him die young so he wouldn’t do that evil and also so he wouldn’t cause suffering to his righteous parents. There are many ways by which a person dies. Sometimes it’s a disease, sometimes an accident, and in this story, Allah commanded one of his servants to kill this boy to avoid that black future.
2) To try his parents. It was a divine trial for them