The U.S.-backed buildup of this country's military forces, human rights reforms and the electoral triumphs of President Jose Napoleon Duarte's Christian Democratic Party have combined to calm the turmoil that beset this country as recently as 18 months ago.

The government appears to have gained the upper hand in the five-year-old civil war with the left-wing Farabundo Marti National Liberation Front. The guerrillas have not had a series of advances since they occupied more than 80 communities during their autumn 1983 offensive.

"The light at the end of the tunnel is going out for them," the armed forces' chief spokesman, Maj. Carlos Aviles, said.

In addition, the armed forces gradually have curbed the use of brutal tactics against the left since 1982. Dawn no longer regularly reveals a corpse or two on roadways around the capital.

Bolstering the rejection of fascist-style violence, which has claimed tens of thousands of lives in the civil war, has been the voters' endorsement of the Christian Democrats in three successive elections. The party's democratic credentials have lent prestige to the government here and abroad as Duarte cautiously has battled human rights abuses and opened a dialogue with the rebels.

With the left on the defensive militarily and the president's far right-wing opponents discredited at the polls, both the government and the U.S. Embassy see a continued favorable outlook despite some concern about the economy.

"In the political terrain, I don't see any serious problems in the future," Minister of the Presidency Julio Rey Prendes said. U.S. Ambassador Thomas Pickering, in his farewell speech yesterday to the American Chamber of Commerce before leaving to become ambassador to Israel after 20 months here, said that his longstanding "guarded optimism" in public has become "much less guarded and much more optimistic."

The historic roots of the seeming turnaround can be traced back many years, to the 1982 elections and even to the 1979 reformist military coup, according to Salvadoran and U.S. officials and other political observers. They pointed to these turning points in the past two years:

* A major shake-up of the armed forces' senior officer corps in late November 1983 consolidated in power what a Christian Democratic official called "a democratic, professional core group" in the military. The personnel shifts came several months after an apparent change among Reagan administration policy makers in favor of applying more pressure for a crackdown on right-wing violence, according to U.S. Embassy contacts.

"They suddenly came to realize in Washington in about the summer of 1983 that you can't win the war unless you do something about the death squads, because the death squads are what feed the guerrillas' strength," a well-placed source here said.

* Duarte's election as president in two rounds of balloting in spring 1984 put control of the national government in the hands of the political center. Duarte's ascent was crucial in obtaining increased U.S. military and economic aid from Congress, and a much-feared violent right-wing backlash failed to materialize.

* The Christian Democrats' unexpected repeat victory in nationwide legislative and municipal elections last March 31 toppled the conservatives from control of the legislature and town halls. When the armed forces' high command went on nationwide television to block right-wing efforts to annul the election, the Christian Democratic official said, "It was the culmination of a process in the Army."

Salvadoran and U.S. officials acknowledged that much remains to be done. The peace talks begun last October with the leftist guerrillas show no sign of ending the war soon, as the government is resisting rebel calls to schedule a third public round. Duarte instead has proposed a series of "private conversations" outside the country to determine if progress is possible at a subsequent formal meeting.

"We don't know how long these preliminary conversations would last," Rey Prendes said.

In addition, security in the countryside has improved only slowly. Air Force medical evacuation helicopters have been flying back and forth low over the capital this week, often ferrying Army wounded to a military hospital. Many passengers are victims of mines that the guerrillas are planting in increasing numbers on trails in the mountains, and the rebels continue to disrupt road transportation and to knock out electrical power lines as part of their economic sabotage campaign.

Another cause for concern is increased use of terrorist tactics by the left. The rebels have stepped up hit-and-run assassinations of military personnel and kidnaped 17 civilian mayors this spring. Two mayors were killed.

Right-wing political violence has not disappeared, with several union leaders sympathetic to the left having been grabbed in recent months. A much-needed overhaul of the judicial system barely has begun, and some arch-conservative sectors of the armed forces still are resisting the reformist trend.

The military is saying openly for the first time that its policies include the forcible transfer from guerrilla-dominated zones of the rebels' civilian supporters. More than 1,000 such persons have been "evacuated" this year by the armed forces as part of an effort to deprive the rebels of their support base, military spokesman Aviles said.

Finally, the economy has stabilized with the help of a massive influx of U.S. aid, but it is not rebounding strongly. The government estimates unemployment to be 50 percent. Approximately 20 percent of the population of 5 million have fled their homes and are living as refugees either in El Salvador or abroad.

"The economy still presents a difficult and unhappy picture," Pickering said.

Fueling uncertainty over the economy were the longstanding differences between the Christian Democrats and the nation's business community. The private sector still mistrusts the Christian Democrats because of the party's support for the 1980 land and banking reforms.

Pickering appealed in his speech for cooperation, but the Christian Democrats were not sounding particularly conciliatory. The government says it will try to funnel credit and other government assistance primarily to small businesses, which give the party greater political support than do the wealthy.

"In the past they the rich controlled the government as they wanted. Now there has been a change in political power," Rey Prendes said.

Despite these notes of caution, however, the improvements in El Salvador seem to be emerging as the Reagan administration's principal accomplishment in Central America, political and diplomatic observers said.

Elsewhere on the isthmus, Washington has confronted but failed to dislodge Nicaragua's left-wing Sandinista government. In Honduras, the administration has built up a force of anti-Sandinista guerrillas and erected a network of military installations, but the policy there increasingly has run into trouble. Tensions have arisen between the Honduran government and the Nicaraguan rebels, and a constitutional conflict between President Roberto Suazo Cordova and the legislature has clouded the outlook for the November elections.

In something of a historical irony, the conservative U.S. administration seems to have achieved many of its goals in El Salvador by gradually coming to accept relatively liberal policies, according to the political and diplomatic observers. It turned out that way largely because the administration found that support could not be obtained in Congress for the battle against the left unless the far right's abuses were stifled, they said.

Looking back over the history of the civil war here, U.S. officials cited three particularly important U.S. interventions against the right.

First, the Carter administration supported the 1980 land and banking reforms, which robbed the left of much of its program.

Then in 1982, Reagan's first ambassador, Deane Hinton, personally stepped in to prevent arch-conservative Roberto D'Aubuisson from assuming the presidency after the March elections even though the rightist's coalition had defeated the Christian Democrats at the polls.

Finally, in late 1983, Washington insisted on curbing the death squads.

This U.S. pressure, combined with Duarte's growing popular appeal, has left the far right in disarray. D'Aubuisson's Nationalist Republican Alliance began to break up after Duarte beat D'Aubuisson for the presidency a year ago.

D'Aubuisson's running mate in that election, Hugo Barrera, now is forming his own party favoring less extremist policies. Right-wing businessmen have backed off from supporting D'Aubuisson as it has become clear that Duarte has more clout in getting U.S. aid.

"The people who gave D'Aubuisson money so that he could become El Salvador's Hitler have failed," a Christian Democratic official said.

Duarte has proved to be a patient, able politician. He has retained the military's support by insisting in the peace talks that the left can come to power only by competing in elections under the existing constitution, and by avoiding so far prosecution of military officers suspected of past involvement with right-wing terror. Such officers generally have been sent abroad to what are called here "golden postings," as military attaches or on foreign study programs.

Many observers credited Duarte's personal courage and tenacity for his success under trying circumstances. He returned to the country from exile in 1980 at the height of the far right's repression even though many conservatives considered him a communist. He doggedly pursued power by way of the democratic process even though the armed forces overturned an election he had won in 1972 and had him beaten and sent into exile.

"Tenaciousness is what makes Duarte. It's not that he's a wise man or the smoothest operator, but his enemies have to admit that Duarte doesn't quit," a senior diplomat here said.