The US Marine Corp hymn begins with the lines of, "From the halls of Montezuma to the shores of Tripoli..." Now the halls of Montezuma of course refer to the capture of Mexico City during the Mexican War, a conflict that can be best described as a grown man beating up a ten year old boy to steal a dirt covered lollipop, but what the hell is the shores of Tripoli about?
At the time America was a brand spanking new country, still in swaddling clothes, the Barbary pirates were the scourge of the Mediterranean and eastern Atlantic. The Barbary pirates hailed from what was at the time called the Barbary Coast, which was a series of kingdoms stretching from Morocco east to modern day Libya. A significant portion of these kingdom's economies were based on piracy. The pirates would seize goods and enslave crews, and many European nations paid them tribute to keep their ships safe. The only nation that didn't take shit from the Barbary pirates was England, and that was because they had the world's largest navy to back them up. This worked great for the 13 American colonies, up until the time they decided they didn't need England anymore. With the protection of the Royal Navy gone, the Barbary pirates fell on American ships with a gusto.
The new American government was less than happy with this situation, and in 1786 sent envoys to the Barbary States where they managed to sign a treaty of friendship with Morocco, which is today America's oldest non-broken friendship treaty. However, the other Barbary States of Algiers, Tunis, and Tripoli were less than amicable. To protect its shipping, the US was forced to pay an annual tribute which ballooned to one sixth of the national budget by 1795. Finding the situation less than great, the US began building a larger navy. In 1800, Thomas Jefferson was elected president, partially thanks to his platform of "we aren't going to take this pirate ships anymore." The moment he became president in 1801, the US stopped paying tribute.
The Barbary War was about as exciting as one could expect. It mostly involved American naval ships doing raids against the cities of the Barbary Coast, sinking a few fishing boats, blowing apart a few buildings, and skedaddling back to safe ports. The highlight of the war was in 1804 when a large US frigate ran aground near Tripoli and was captured, but was later blown up by a group of Marines. In 1805 the war finally came to a conclusion after 8 US Marines, plus 500 or so Greek and Arab mercenaries, marched out of Egypt and captured the port city of Derna, which was owned by Tripoli. This is the battle referenced in the Marine Corp hymn. Tiring of being pestered all the time, the Barbary States signed a treaty with the US agreeing to stop raiding their shipping.
Soon after the Barbary War, world attention shifted to the growing war between England and France, which for the US, culminated in the War of 1812. During this period the Barbary pirates renewed their raids just two years after signing the treaty. This resulted in the Second Barbary War of 1815, where the now much larger US navy heavily bombarded the Barbary Coast until a new treaty was signed, this time for keeps.