These were the Ottoman provinces of Tripoli, Algiers, and Tunis, which were enjoying a large autonomy, as well as the independent Sultanate of Morocco.
The war was fought because U.S. President Thomas Jefferson refused to pay the high tributes demanded by the Barbary states and because they were seizing American merchant ships and enslaving the crews for high ransoms.
It was the first declared war the United States fought on foreign land and seas.
In 1786, Jefferson and John Adams met with Tripoli’s ambassador to Great Britain. They asked this ‘diplomat’ by what right his nation attacked American ships and enslaved her citizens and why the Muslims held such hostility toward this new nation, with which neither Tripoli nor any of the other Barbary Coast nations had any previous contact. The answer was quite revealing.
Sidi Haji Abdul Rahman Adja (the ambassador) replied that Islam:
“Was founded on the Laws of their Prophet, that it was written in their Qur’an, that all nations who should not have acknowledged their authority were sinners, that it was their right and duty to make war upon them wherever they could be found, and to make slaves of all they could take as Prisoners, and that every Musselman (Muslim) who should be slain in battle was sure to go to Paradise.”
Does any of this sound familiar? 200+ years later they are saying the same thing and acting accordingly. In fact, they have been doing that for 1400 years in something like 600 wars. Why do people think they are going to change and be nice people? Their true colours were revealed in Syria, Iraq, Afghanistan, Pakistan, Nigeria, Somalia, Paris, Brussels, New York, London, Spain, etc., etc. It's just a matter of time before their colours show in EU countries that foolishly let them in with little discernment. Why are do-gooders so colour-blind?
That is indeed quite revealing. Yet, America continued paying ransoms to these terrorists for the next fifteen years or so, until Jefferson became President.
Then, the Pasha (leader) of Tripoli sent a demand to the new leader for an immediate payment of $225,000 and $25,000 per year on an ongoing basis. Jefferson flatly refused, leading the Pasha to cut down the flagpole of the American consulate and declaring war on the United States. The rest of the terrorist states followed suit.
As they saw the US was actually committed to the fight, Algiers and Tunis quickly abandoned the war and allegiance to Tripoli.
Obviously, the US won the war. In fact, this was the reason why the line “to the shores of Tripoli” was added to the Marine Corps hymn.