The Kingdom of Morocco formally recognized the United States in 1777. Why did they do this? What did Morocco have to gain from recognizing the United States so early? Did the fledgling United States benefit at all from this early recognition?
Well Mohammed III, the then king of Morocco, gave protection to the U.S. merchant ships against the Barbary pirates that plagued the seas, due to America's weak position at that time and having almost no navy. They also had the right to enter the ports and trade, just like any other country. Morocco was the first to set up trade with the U.S.A. They still have a Free Trade Agreement which was made in 2006.
On 20 december 1770, 1.5 years after the U.S. declaration of independence, did Morocco recognise, as the first country in history, the independence of the U.S.
In 1787 they made a treaty of friendship, which stands now the longest unbroken treaty of friendship of the U.S.
Also during the Civil War did Morocco supported the Union, and imprisoned two diplomats of the Confederacy, by the name of Henry Myers and Tom Tate Tunstall, after the consul himself asked them imprisoned.
So what did they get out of it? Firstly it was protection and trade for the U.S. when it was still a young country, and trade for Morocco.
Later on it was the U.S. that kinda "protected" Morocco in the time of colonising, a lot of European states wanted Morocco for their own, and the U.S. calmed the situation and made a deal granting Morocco way more autonomy than the other African countries, while also being able to trade with the European countries. But in 1912 Morocco became a protectorate (colonized) by Spain and France anyway. So the U.S. again "protected" by calling upon racial & religious tolerance, which was absent for many of the other countries. It should be noted that while the voice of the U.S. did help, Moroccans still greatly suffered under colonial rule albeit way less than its neighbour Algeria.
In WW1 Moroccan troops fought alongside U.S. Marines in many a battlefield and in WW2 they were formally with the Axis powers yet thats because of France having fallen. When the Allied powers entered Morocco in '42 Morocco was quick to surrender and work with the Allied.
During the Cold War Morocco was officially non-allied, but was one of the only Muslim States rooting for the West.
In the conflict over the Western Sahara, the U.S. has sided with Morocco with various presidents voicing their support.
Last but not least, the Moroccan Secret Services, one that is renowned for how fast they uncover plots, has been trained by both the Mossad and the CIA, and it's where the CIA gets their Arabic speaking spies from. Morocco was quick to stand with the U.S. against terrorism and they support each other on it.
As you can see, Morocco and the United States of America go way back in their friendship and history. I always found it interesting as to why. Trade, protection and common interest have been the reason.