On the evidence of recent events, Morocco's King Hassan II has won a high-risk political gamble: that he would be able to move closer to Libya without jeopardizing his country's close military and economic ties with the United States.

The Reagan administration was surprised and taken aback last August when Morocco, a conservative North African kingdom that traditionally has played a moderating role in Arab politics, signed a "treaty of union" with Col. Muammar Qaddafi's Libya, viewed by Washington as a nation that encourages international terrorism. There was talk in Washington of punishing King Hassan by cutting American military and economic assistance to Morocco, which is embroiled in a costly, 10-year-long guerrilla war in the Western Sahara.

Less than eight months later, Moroccan officials are congratulating themselves that no U.S. aid program has been adversely affected. The Reagan administration has signaled its intention of maintaining good relations with Morocco by dispatching a string of high-level emissaries to King Hassan.

The United States was the only country to be represented at the annual feast of allegiance to the Moroccan throne earlier this month by no less than three presidential envoys: U.S. Ambassador to the United Nations Jeane J. Kirkpatrick; her designated replacement, Gen. Vernon Walters, and the permanent U.S. ambassador in Rabat, Joseph V. Reed.

The latest U.S. gestures toward King Hassan reflect both the U.S. view of the strategic importance of Morocco and the practical difficulties involved in implementing the Reagan administration's policy of isolating Qaddafi diplomatically. During the past year, the leaders of three West European countries allied to the United States -- France, Italy and Greece -- have had personal dealings with the mercurial Libyan leader.

Questioned about U.S. unhappiness with the Moroccan-Libyan treaty of union at a news conference after the festival, Hassan replied sharply: "Before being a friend of the United States, I am first of all the king of Morocco."

Senior Moroccan officials argue that the main purpose of last year's agreement with Libya was to strengthen Morocco's hand in its war against the Algerian-backed Polisario Front in Western Sahara. Libya was once a major supplier of arms and funds to the Polisario guerrillas who have set up their own Sahrawi Arab Democratic Republic.

By agreeing to the treaty of union, in practice a politically loose federation that allows for economic and cultural cooperation, the Moroccan monarch has managed to tilt the regional balance of power in the Maghreb back in his favor. Until last August, Algeria seemed to be succeeding in its aim of outmaneuvering Morocco by concluding alliances with both Tunisia and Mauritania.

Asserting that a "misunderstanding" with the United States over the treaty with Libya had been cleared up, Hassan said, "anybody can see that in the application of this agreement, neither Libya nor Morocco has given up their previous policies nor renounced their previous friendships."

Some political analysts here said the shock King Hassan produced in Washington by failing to give the United States advance warning of his opening toward Libya could have worked to his advantage here. It was seen as a way of demonstrating that, although his government is closely identified with the West, he is in no way an American puppet.

Moroccan officials made clear that the king intends to go through with the second stage of the treaty of union which includes the setting up of a joint secretariat and parliament. Hassan II is expected to visit the Libyan capital, Tripoli, over the next few weeks.

Under a joint military protocol negotiated in 1982, the United States has the right to use two Moroccan air bases for its Rapid Deployment Force in the event of an international crisis such as a sudden flare-up in the Persian Gulf. Moroccan beaches are used for joint military exercises involving both Moroccan troops and U.S. forces stationed in Europe.

Occupying a strategic position controlling the southern approaches to the Strait of Gibraltar, Morocco has opened its ports to visits by the U.S. fleet. Recently Charles Z. Wick, director of the U.S. Information Agency, formally took possession of the site of a new transmitting station for the Voice of America in Tangiers that U.S. officials say will be the largest such facility in the noncommunist world.

U.S. economic and military assistance to Morocco has expanded since Reagan's election in 1980, following a period of strained relations under the Carter administration, but still falls short of American assistance to such countries as Egypt. Total U.S. aid to Morocco is now running at around $400 million a year, compared with about $50 million in 1980.

Morocco relies on U.S. and French military supplies and ammunition to keep the war in the Western Sahara going, although U.S. officials deny that the aid is specifically tied to the conflict.

In the Moroccan view, the aid represents meager recompense for the service Morocco renders the West. A recent editorial in the official government newspaper, Le Matin, called on President Reagan to "respect" a promise made to a former Moroccan sultan by George Washington in 1787.

Thanking the "great and magnanimous sultan" for his friendship and support, Washington acknowledged that the United States was a fledgling nation worn down by war, but added -- optimistically, it must have seemed at the time -- "our soil is bountiful and our people industrious and we have reason to flatter ourselves that we shall gradually become useful to our friends."