Thank you for contacting About Islam with your question.
Dr. Shabir Ally addresses this question in the video below:
Aisha Khaja: So, Dr. Shabir, the question that we have is: if it is bad and evil to kill, then why did the Prophet go to war? Also, if he was considered to be a peace preacher?
Dr. Shabir Ally: Well, and yes, it is bad to kill. And yes, the Prophet Muhammad (peace be upon him) was considered to be a preacher of peace.
But at the same time, there are some circumstances in which war is thrust upon you, and you have no way out but to defend yourself in a reasonable way.
And that is what the Prophet Muhammad (peace be upon him) had to do. He struggled for many years preaching Islam in a very peaceful manner.
He was persecuted, so were his followers as well. Then he moved away from that scene of violence.
But still the non-Muslims, wanting to wipe out this new religion and its followers, came battling with the Prophet Muhammad (peace be upon him) in one army after another.
And the Prophet (peace be upon him) had to then bring out his followers from their city to meet the advancing army of the enemy in order to forestall them from entering the city of the Prophet and decimating the Muslim population.
I hope this helps answer your question. Please keep in touch.
Please continue feeding your curiosity, and find more info in the following links: