Socialist leader Mario Soares was elected Portugal's first civilian president in 60 years today, coming from behind to defeat his right-wing opponent by a narrow margin in a second-round runoff.

Soares' remarkable rebound after a sharp Socialist defeat in an October legislative election came as a major setback to the minority center-right government that backed conservative candidate Diogo Freitas do Amaral.

Projected final results gave the 62-year-old Socialist leader a 2-percentage-point victory as he rallied the left-of-center vote, accumulating the support of two left-wing candidates defeated in the first round three weeks ago, when Soares polled 25 percent.

Freitas do Amaral, who fell only 4 percentage points short of winning half the first-round vote for an outright victory, drew an estimated 49 percent of the vote, the largest vote for the right since the 1974 "Revolution of the Carnations" ended a half century of authoritarian rule.

"My election marks the end of our transition to democracy," Soares said. "Now we must concentrate on the struggle against poverty, ignorance and intolerance to meet the challenge of Europe." Portugal joined the European Community Jan. 1.

Soares' election could threaten the survival of the conservative government of Prime Minister Anibal Cavaco Silva, which holds only 88 of the 250 National Assembly seats.

Before the election, Cavaco Silva said only a victory for Freitas do Amaral, a 44-year-old former Christian Democrat leader, would ensure the government the support it needs to embark on a program of reforms aimed at ending state domination of the economy and capitalizing on membership in the European Community.

"We won't give up our efforts to change Portugal," the prime minister said in acknowledging Soares' victory. But he warned that cutting short the life of his four-month-old government, Portugal's 16th administration in 12 years, would carry enormous costs.

Soares has pledged he will not provoke conflicts with the government. "I will be the president of all the Portuguese, not just of my party or the majority that elected me," he said after his victory.

But as the leader of the main opposition party that was toppled from power by Cavaco Silva, he could support the formation of a new left-of-center administration if the conservative government's program is blocked in parliament.

The presidential majority led by Soares does not carry over into parliament, however, where the left is split between the Socialists, the pro-Soviet Communists and the Democratic Renewal Party of the outgoing president, Gen. Antonio Ramalho Eanes.

A government alliance with the Communists is unthinkable to Soares.

Cavaco Silva warned Soares that Freitas do Amaral's 49 percent could be turned into a parliamentary majority for the Social Democratic and Christian Democratic parties that backed the right-wing candidate.

After inspiring confidence with a series of reflationary measures following three years of recession, Cavaco Silva appears assured that an early legislative election provoked by the opposition would bring his government back with a bigger majority.

Soares' victory crowned a long career of struggle against the pre-1974 dictatorship and Communist attempts to seize power in the turmoil of the revolution that returned the country to democracy.

After imprisonment and persecution by the political police of the previous regime, he returned from exile to a hero's welcome after the leftist Army coup 12 years ago. He won crucial backing from successive U.S. administrations when he led street demonstrations in 1975 against a Communist bid to win control of the revolution.

He was Portugal's first democratically elected prime minister. But his three terms were marked by fruitless efforts to forge a social pact between workers and employers and by damaging disputes with the left-of-center Eanes.

After imposing three years of austerity in a successful bid to avert a foreign debt crisis, Soares' Socialists suffered their worst election defeat last October, dropping from 36 percent to 20 percent of the vote. Analysts here said then Soares' political career was finished.

But when he appeared before cheering crowds in Lisbon tonight, the Socialist leader appeared to have regained the stature of the tireless leader of the democratic revolution.

The runoff campaign turned into a classic battle between left and right that overrode the complexities of Portugal's fragmented political spectrum. Soares, who has drawn fire from the left for "putting socialism in a drawer" when in power, portrayed himself as a workers' champion and attacked his opponent for threatening the freedoms won in the 1974 revolution.