The U.N. General Assembly today voted 104 to 18 to demand withdrawal of foreign troops from Afghanistan, dealing the Soviet Union its most stunning diplomatic defeat since the world condemned its 1956 invasion of Hungary.

Those voting against the resolution for withdrawal included only what one startled observer called the "inner circle" of countries overtly in the Soviet camp. The only "no" vote outside the recognized communist world came from the Caribbean island of Greneda.

Another 18 delegations of the 148 eligible to cast ballets abstained. They included Algeria, the Congo, Finland, Nicaragua, Madagascar, Syria and Zambia.

Today's vote came less than two weeks after a group of 53 nations, including the United States, requested a Security Council meeting on last month's invasion. On Jan. 7, the Soviet Union vetoed a council resolution demanding its withdrawal from Afghanistan, leading to a call for a special emergency assembly session.

The special emergency provision, invoked only six times in the United Nation's 34-year history, allows no veto. Today's resolution received more than the two-thirds majority necessary for passage of questions affecting international peace and security.

Diplomats acknowledged that the resolution, sponsored by 24 Third World nations, contains no direct provision for enforcement and is unlikely to result in the removal of any of the Soviet Union's estimated 85,000 troops now in Afghanistan.

But several open-ended clauses -- calling for the issue to be returned to the Security Council "to consider" ways to help implement the resolution and arging members to help bring about "the voluntary return of the Afghan refugees to their homes" -- may enable further action.

In four days of assembly debate beginning last Thursday, at least 75 nations spoke on the issue. Many nations traditionally friendly to the Soviets said they had difficulty supporting the invasion.

Among the most surprising speeches was that of Cuban Ambassador Raul Roa, who today called the situation a "historical dilemma." Although he voted against the resolution, Rosa acknowledged that Cuba had "highly esteemed friends" who honestly believe this exercise is concerned with . . . the independence and sovereignty of peoples."

Roa made no mention of the invasion, and offered no defense of the Soviet action. Instead, he said Cuba would never vote with the United States on any issue. "We shall never carry water to the mill of reaction and imperialism . . . against socialism with the accomplices of Zionism," he said.

The Soviet position through both the Security Council and assembly debates was that the now-deposed government of Afghan president Haflzullah Amin had invited the troops to protect Afghanistan from "armed aggression" of "imperialist circles."

The Soviets have charged the United States and China with training and arming Afghan insurgents who attack the communist government in Kabul from bases inside the country and across the border in the Pakistan.

Although Amin was assassinated shortly after the Soviet troops arrived. Afghanistan's new government, which the West charges was Soviet-installed told the United States it welcomed the Soviet presence. Both the Soviet Union and Afghanistan described the issue as an internal matter, within the provisions of a bilateral treaty, and of no concern or threat to the international community.

Soviet Ambassador Oleg Troyanovsky, quoting remarks made during the weekend by President Leonid Brezhnev, today blamed what he called the "deliberate and brazen distortion of the Soviet role in Afghanistan' on U.S. attempts to revive the Cold War.

Troyanovsky called U.S. economic sanctions and military buildups in response to the invasion a "serious policy miscalculation" and a U.S. administration "boomerang" that will "strike back, if not today then tomorrow."

He characterized the United States as a "completely unreliable partner in interstate relations" whose foreign policy is ruled by "whims, caprices and emotional outbursts.

Throughout the debate, the United States strived to let Third World countries take the lead in opposing the Soviets and to portray the issue as one between the nonaligned movement and the Soviet Union, rather than a big-power struggle.

Many Third World countries had refused to take sides in a similar debate over economic sanctions against Iran. There was little outcry when a U.S. osanctions resolution was vetoed by the Soviets in the Security Council Sunday night.

But the majority today echoed Sri Lanka's ambassador B. J. Fernando in discounting the Soviet explanation for its troop presence as absurd and expressing "not a sense of confrontation of outrage, but rather . . . genuine feelings of anxiety and concern" about events that could escalate and "seriously set back" the developing nations.

In a statement that reflected the feelings of most of the Third World centrist, governments, Mexican Ambassador Porfirio Munoz Ledo noted that "the decade of the '80s is beginning badly" for them.

One indication of the problems some leftist governments had with the Soviet position was an attempt last night, reportedly led by Algeria and others, to introduce a substitue resolution that would call on both super-powers to act to "relieve tensions."

Sponsors of the original resolution, however, refused to yield in a caucus this morning. They objected to a substitute provision recognizing "the right of every state to take all steps it considers necessary . . . to preserve its independence, sovereignity and territorial integrity" as a loophole that could justify the Afghan invasion.

A number of countries have made clear despite today's overwhelming condenmation, that they have no wish for military action to be taken against the Soviets. Several including France, specifically noted that their approval does not include acceptance of a U.N. provision allowing the international body to send peacekeeping troops in such a situation.

Voting against the resolution were: Afghanistan, Angola, Bulgaria, Byelorussia S.S.R., Cuba, Czechoslovakia, South Yemen, Ethiopia, East Germany, Grenada, Hungary, Laos, Mongolia, Mozambique, Poland, the Ukrainian S.S.R., the Soviet Union and Vietnam.

Those abstaining in the vote were Algeria, Benin, Burundi, Congo, Cyprus, Equatorial Guinea, Finland, Guinea, Guinea Bissau, India, North Yemen and Zambia.