In a major attempt to reassert Soviet influence in the Middle East, the Soviet Union and Syria tonight signed a 20-year treaty of friendship and cooperation that binds them in a close military and economic relationship.

The treaty calls for continued "cooperation in the military field," the official news agency said, language virtually identical to similar treaties the Soviets have signed over the years with Iraq, Afghanistan and other Third World nations.

The pact also calls for mutual consultation on threats to each other's security, or violations of peace and security "in the whole world," a clause never seen before in such a document, according to some observers here. The two capitals are to "coordinate their poositions and [cooperate] in order to remove the threat and restore peace."

Although heavily dependent on the Soviet Union for military and economic assistance already, Syrian President Hafez Assad up to now had avoided binding his country to a formal pact with the Soviets and in recent years had pursued a course that frequently caused friction with Moscow. For this reason, the treaty is sure to arouse concern in Washington, where fears of expanded Soviet influence because of the Iran-Iraq war already are running high.

Soviet President Leonid Brezhnev restated Moscow's neutrality in a toast honoring Assad, saying: "We are not going to intervene in the conflict between Iran and Iraq. We stand for its earliest political settlement by the efforts of the two sides." But, reflecting vital Soviet interests in the region, he directed an implicit warning to the Carter administration by saying, "We resolutely say to others: hands off these events."

The new treaty appears to raise complex diplomatic problems for the Russians who have an almost indentical pact with Iraq, a neighbor of Syria and a bitter foe of the Assad government. Two days ago, the Syrians publicly assailed Iraqi leader Saddam Hussein as an "agent of imperialism." To complicate matters further, the Soviets are striving to maintain good relations with their neighbor Iran as Tehran and Baghdad enter the third week of their war.

But the Soviets appear to have decided to exploit current uncertainties in the Middle East to establish a solid footing in the region irrespective of the outcome of the war. The Syrians, also concerned about the situation, seem to have entered into a formal alliance with Moscow to protect their interests should the regional situation deteriorate farther. The Syrian treaty with the Soviets follows a recent agreement to enter a formal union with Libya, which also has close ties to Moscow.

With increasing bitterness, the Soviets have accused the United States and its allies of preparing military action to intervene in the conflict. Two U.S. carrier task forces are operating in the vicinity of the Persian Gulf and the United States has offered limited assistance to any country in the region fearing for its own security and willing to remain neutral in the Iran-Iraq conflict. Only Saudi Arabia has so far accepted.

Although Brezhnev sought to soften the implications of the treaty by asserting that it "is not directed against third countries," Assad, in a virulent attack on the United States and Israel in his Kremlin toast, declared the pact is "aimed at" assuring the "aggressors' withdrawal from the occupied Arab lands, to ensuring the inalienable rights of the Arab Palestinian people and first of all its right to return, to self-determination and the establishment of an independent state of its own on the soil of its native land."

Although the pact outlines various points of economic and technical cooperation, it is the military and mutual security clauses that have proven most potent in the volatile Middle East and gulf region. The Soviets cited the mutual security clause in their treaty with Afghanistan when intervening there last December, and Moscow has indicated it would continue to supply arms to Iraq in its war with Iran under the 1972 Moscow-Baghdad pact.

Similar treaties have been signed by Moscow with India, Angola, Mozambique, Ethiopia and Vietnam. Egypt and Somalia cancelled their friendship treaties after sharp differences with the Kremlin. Egypt, which signed the Camp David treaty with Israel, has become a firm U.S. ally, ostracized by other Arab states. Somalia and Washington are seeking closer ties, in part because of U.S. interest in naval bases in the Horn of Africa.

The treaty underlines Moscow's new influence in Damascus, asserting the parties will "develop and broaden" their consultations "above all on the problems of the Middle East."

Brezhnev said "Soviet-Syrian relations are developing steadily, are growing broader and deeper," and the pact raises relations "to a new, higher level." Assad has regularly made autumn visits here in recent years, but always in the past has left without agreeing to a friendship pact despite receiving new Soviet arms and economic assistance. Tonight, Brezhnev implied that the treaty was instigated by Assad, who faces internal unrest from fundamentalist Islamic factions inside Syria. The Soviet leader declared, "We highly value the initiative displayed by the Syrian side and you personally, comrade president."

The pact was quickly signed only hours after Assad arrived here. Last week, during a visit here by Indian President N. S. Reddy, Brezhnev criticized the United States without ever naming it. "The imperialists would like very much to strike a crushing blow to Arab unity, to use each of warring sides for their aims and to re-establish their domination in Iran. The Soviet Union has given and will continue to give a strong rebuff to such a policy. And we are glad that in this respect we hold a common stand with Syria and other really peace-loving states."

Brezhnev again accused the United States tonight of "collusion of plunder, aggression, . . . blackmail," and said "no one has the right to meddle from outside" in the affairs of Persian Gulf nations. He condemned Israel anew, saying all "lands seized from the Arabs by Israel in 1967, including the eastern part of Jerusalem, should be unconditionally returned to them" so a lasting regional peace could be achieved.

"It is impossible to insure the security of some people by trampling the rights of others. It is high time for the leaders of Israel to understand this simple truth," Brezhnev said.

The treaty signing opens a spurt of Soviet diplomatic activity in coming days. Afghan Marxist leader Babrak Karmal is expected here soon, his first trip from Kabul since he was installed there by Soviet troops last December. tThe Kremlin also anticipates a trip here by Jordan's King Hussein, who is sure to be both wary of the implications of the new treaty and to be interested in pursuing his increasingly activist role as an Arab spokesman and possible Islamic mediator.