All praise be to Allah and all blessing be upon the Prophet Muhammad ﷺ.
The word Islam comes from the root word: Salaam which means peace. It also means submitting a person’s will to Allah. So Islam basically means acquiring peace by submitting one’s will to Allah. The fact that the Prophet ﷺ engaged in so many military campaigns doesn’t negate that Islam is a religion of peace. It is a religion by which people can acquire ultimate peace both in this world and the next.
Muslims lived in Makkah for 13 years without indulging in any sort of violence. If Islam was a violent religion, then the Prophet ﷺ and the early Muslims could have attacked the Quraish in Makkah and they would have been successful in assassinating many of the leaders of the Quraish such as the Abu Jahl, Omaiyyah ibn Khalaf, etc. However this is not the goal of the religion and it is only to worship Allah without associating partners with him.
This can be seen in the verse of the Qur’an in which Allah says: Have you not seen those who were told, “Restrain your hands [from fighting] and establish prayer and give zakah”?. This verse shows that there was no permission to fight or even retaliate during the period of Makkah for 13 years. One could see Bilal being dragged on the street, Khabbab being branded by fire, carcasses being thrown on the Prophet ﷺ or he being throttled , and various other atrocities but yet one could not raise a finger, one could not even curse them, since the permission wasn’t there.
Moreover, as time passed and the atrocities and the crimes committed by Quraish were increasing day by day, the Prophet ﷺ instead chose to do hjrah instead of fighting them back. If he would have wanted he could have secretly planned assassination attempts and do away with the leaders.At the second pledge of Aqabah, when the oath was being taken, an Ansari said to the Prophet ﷺ, “By Allah, who has sent you in truth, we are powerful enough to put the people of Mina to our swords tomorrow, if you desire.” The Prophet replied that “We have not been commanded to follow that course”. This is a clear evidence that violence is not the goal of Islam, if it had been, then the prophet ﷺ could have ordered them to kill the leaders of the Quraish and Muslims would have been victorious in the early stage itself.
The wars which took place during the Madinan phase were only done once an Islamic state was established. Islam does not support a vigilante system of justice. A war or a battle cannot be announced by any normal Muslim, but it is only to be announced by the leader of the State.
Once he reached Madina, he also made a covenant which is called the Saheefah, with the Mushrikeen and the Jews. He almost gave them independence to practice their own faith and establish their laws. However, the threat of being attacked always loomed on the horizon for the nascent state. Hence the Prophet Muhammad ﷺ started sending out envoys and he planned to attack the Quraish only. This is was done because the Quraish were the ones who tortured the Muslims for 13 years , who drove them from their homes, who looted the properties of the Muslims, who killed many Muslims and various other atrocities. It was totally justified that Muslims now retaliate.
Allah Says in the Quran : Permission [to fight] has been given to those who are being fought, because they were wronged. And indeed, Allah is competent to give them victory. The Goal of Islam is not to engage in war; hence the wording of the Qur’an is that, ‘Permission has been given’, since Muslims wanted to get back at the Quraish for all they had done.
Hence Badr took place, although both the parties did not intend to fight each other and the Muslims became victorious in that battle. Similarly Uhud took place because the polytheists wanted to take revenge for their defeat at Badr. This was also the case for the Battle of Khandaq, where 10,000 people marched towards Madina in order to destroy it totally from its root. However, they were unable to do so. Hence many of the wars were also fought in Self Defense and the prophet ﷺ fought the Quraish because he was forced to. Moreover, many a times he also attacked because for a state to be secure, sometimes attack becomes the best defense. This can be seen in various battles like Banu Al Mustaliq, Tabuk, Hunain, etc.
When we look at the siege and the battles which took place with the Jewish tribes, like the Banu Qainuqa and the Banu Nazeer, if the Prophet ﷺ wanted he could have been harsh towards them, since they broke the covenant and Banu Nazeer even tried to assassinate the Prophet ﷺ . However they were ordered to be exiled and these two tribes left Madinah to settle somewhere else. While Banu Quraizah deserved their punishment for trying to plot and attack the Muslims from inside during the Battle of Khandaq. They were war criminals and no country or state can accept traitors. Even when these criminals were killed, the women and children were not harmed.
Similarly if we look at all the other military campaigns they were carried out with strict rules and regulations. No women, no elderly, no children are to be harmed and various other rules were also put in place to ensure the objective of the campaign is fulfilled. These campaigns were not like the modern wars in which pure looting, banditry and other heinous crimes are committed. The main goal of Jihad is to make people worship Allah and there should be no oppression and injustice in this world. Allah says in the Quraan : And fight in the way of Allaah those who fight you, but transgress not the limits. Truly, Allaah likes not the transgressors”.
One of the most important reasons is also that even though the Prophet ﷺ had to engage in so many conflicts due to various reasons, we clearly see that the number of casualties is just little more than 1,000. If Islam was a violent religion then the number of casualties would have been more, but the casualties itself show that killing people, terrorizing them is not the goal but to make them realize that Allah is only one who is deserved to be worshipped and all acts of Ibadah belong to him alone. When we compare them with the Modern Wars, especially the world wars, where millions and millions of people die, to compare Wars of the Prophet Muhammad ﷺ with those wars is injustice.
Islam is not a pacifist religion. Islam permits people to fight in the way of Allah as Allah says in the Qur’an :” [They are] those who have been evicted from their homes without right – only because they say, “Our Lord is Allah .” And were it not that Allah checks the people, some by means of others, there would have been demolished monasteries, churches, synagogues, and mosques in which the name of Allah is much mentioned. And Allah will surely support those who support Him. Indeed, Allah is Powerful and Exalted in Might.” 
Also Allah says that: “And if it were not for Allah checking [some] people by means of others, the earth would have been corrupted, but Allah is full of bounty to the worlds.”  However, War is the last resort and is subject to rigorous conditions laid down by the Sharia.
Hence we see that for the above reasons, like for the safeguard for Muslims, self defense, pre empting the attack, etc Prophet Muhammad ﷺ had to engage in military campaigns.
And Allah Knows best
 Surah Nisa 4:77
 Page 75, The Sealed Nectar, Safi ur Rahman Mubarak Puri
 Surah Hajj, 22:39
 Surah Baqarh 2: 190.
Surah Hajj 22:40
 Surah Baqarah 2:251
(n.d.). Retrieved from http://islamqa.info/en/34647