The Soviet Union, which has just been thrown out of Somalia, appears to be losing ground here in Marxist Mozambique as it turns toward more contacts with the United States and other Western nations.

Soviet military aid to bolster Mozambique's meager defenses against repeated Rhodesian air and land attacks has not come in the quality or scale anticipated nine months ago, according to Western and Mozambican sources.

The unexpected turnabout in Soviet fortunes in this sprawling Indian Ocean nation could seriously set back Moscow's drive to broaden its influence throughout southern Africa, at Western expense, taking advantage of the Rhodesian dispute and racial conflict in South Africa.

Mozambican leaders had been concerned about the consequences of the large Soviet or Cuban presence in Somalia and Angola, according to Western diplomats, who also note the intense nationalist spirit here born out of a decade of struggle against the former colonial power, Portugal.

Located on the borders of both Rhodesia and South Africa, Mozambique obviously is regarded by the Soviets as an extremely important ally in their southern African effort.

Last April, Moscow signed a treaty of friendship and cooperation with Mozambique during what appeared to be a highly successful visit by Soviet president Nikoli Podgorny. iIt was only the third such Soviet pact with a black African nation, following those with Somalia and Angola.

The treaty also marked a victory in the Soviet struggle with China for friends and influence in black Africa. Just after Mozambique's independence in June, 1975, China was regarded as the most favored foreign power here.

Today, however, the Mozambican government seems to be the turning more toward the West in its search for broader diplomatic and economic support.

At their New York meeting in October, President Carter and Mozambican President Samora Machel declared the advent of a "new era" in theretofore chilly relations. Machel also let it be known that his government was interested in American investment. His minister of industry met six top U.S. executives in New York.

Since then, President Carter has sent at least one private message to Machel, expressing his support for Mozambique in the wke of the latest Rhodesian attacks.

Despite the "opening to the West," Western diplomatic sources and other local observers here see no indication that Mozambique is abandoning its commitment to building a Marxist state and society. They believe, however, that there is every sign it intends to steer clear of entangling alliances with the Soviet Union and to maintain a credible nonaligned posture allowing it to cooperate with both the West and the East.

This shift may be explained in part by Machel's friendship with Somali President Mohamed Siad Barre and Angolan President Agostinho Neto. All three countries are allied in the radical socialist African camp. Somalia ordered out the Soviets after they gave extensive military aid to Ethiopia, Somalia's rival on the Horn of Africa.

While Mozambique refrained from inviting the Soviets or Cubans in here, Western diplomats say that constand Rhodesian incursions, often deep into the country, are testing Mozambican self-reliance. Some feel the "day of the Cubans" may not be far off if the raids continue.

So far, Machel has relied only on Mozambique's own limited military capability and on African assistance. Tanzania has provided some ground-to-air missiles and elements of two infantry battalions.

Machel is presently on a state visit to Nigeria. Whether or not the question of Nigerian military aid is raised, there are doubts here that even Nigeria - with the biggest army in black Africa - holds the answer to defending against the Rhodesian attacks.

The biggest surprise here is that there is no evidence that the Soviet Union has given Mozambique the military wherewithal to defend itself. During the latest Rhodesian incursion in November, Western reporters were surprised to discover how lightly defended both the country's borders and the Zimbabwe nationalist guerrilla camps inside Mozambique, still are against Rhodesian attack. Zimbabwe is the Africa name Rhodesia.

It appears that Mozambique has little or no air defense to counter Rhodesian jets, bombers and helicopter gunships.

Mozambican and Western diplomatic sources say the Soviets have provided Mozambique with mostly obsolete weapons, such as T-34 tanks and MIG-17 jets.

Other Soviet aid has led to doubts as to which country is benefiting most, the sources say. They noted that the program was also marred by an airline ticket scandal involving the Soviet Union's Aeroflot line that cost Mozambique's airline millions of dollars in scarce hard currency.

The Soviet-Mozambican friendship treaty only states that in "situations tending to threaten or disturb the peace," the two parties "will enter into immediate contact with the aim of coordinating their positions in the interest on eliminating the threat or re-establishing peace." This does not commit Moscow to come to Mozambique's defense.

The Soviet Union also pledged military aid to Mozambique "on the basis of the relevant agreements signed by them in the interest of strengthening their defense capacity."

Soviet sources here said there was no other, secret, military pact. So far, Soviet assistance has been limited to some military instructors and weapons. The Soviet sources said Mozambique has asked for no more, but that help would be sent if required.

There are two theories circulating in diplomatic circles as to why the Soviet aid has been limited.

One holds that Mozambique has not asked for more because it does not want to play into Rhodesia's hands by internationalizing the conflict. Direct Soviet or Cuban intervention could provoke South Africa into providing more military aid to the white-minority Rhodesian government and even involve South Africa directly.

Another factor in Mozambican reluctance to call in the Cubans or Soviets, according to local sources, is that there is a strong tradition within the ruling Front for the Liberation of Mozambique (Frelimo) of fighting the country's battles outside volunteers or experts. Frelimo never allowed foreign personnel to fight for it during its 10-year struggle against the Portuguese, they say.

The second theory is that the Mozambicans did ask for more sophisticated weapons but were turned down. A main reason for this alleged Soviet refusal would be the fact that Moscow is supporting the Rhodesian black nationalist faction based in Zambia, the ZImbabwe African People's Union, while the African National Union, is Chinese-backed.

Thus, the Soviets would not care, and indeed would be privately happy, if the guerrilla force here does not take a pounding since it serves to strengthen the camps in Zambia that so far have escaped large-scale Rhodesian attacks.

The main evidence in support of this theory is that some Mozambique officials have privately expressed their dissatisfaction to Western visitors with the quantity and quality of Soviet arms sent here.