El Salvador's constituent assembly elected banker Alvaro Magana provisional president of the nation today, ending a month of struggle over the division of power by also naming three vice presidents, one from each major party.

The agreement, pressed by the armed forces with strong U.S. encouragement, creates at least the facade of a government of national unity assigned to wage war against leftist guerrillas, agree on the future of existing social reform programs and continue to receive U.S. military and economic aid.

Magana, 56, accused by his opponents of being dangerously leftist, was supported by the largest party, the Christian Democrats, and the third-ranking National Conciliation Party as a fence-mending moderate.

The three vice presidents, in order of their succession to the presidency, are Raul Molina, leader of the traditionally conservative National Conciliation Party; law professor Gabriel Mauricio Gutierrez Castro of the second-ranking and extreme rightist Nationalist Republican Alliance (ARENA from its Spanish initials), and Pablo Mauricio Albergue of the centrist Christian Democrats, who is minister of the presidency under the current military-civilian junta.Deep divisions among these men and their parties will not be healed overnight. It was not certain until minutes before the vote that the fragile alliance between Christian Democrats and National Conciliation would survive a strong challenge from ARENA and others of the extreme right. Howling opponents packed the assembly galleries, waving signs and shouting over what they said was a betrayal of the March 28 election results.

They passed out leaflets that attacked Magana, a Roman Catholic, as "a little Jew" and a communist whose election would be treason.

But leaders of the three warring groups had met last night at a private home with the military high command, who "pleaded more than ordered" that the parties agree so that U.S. aid would continue, according to one party official.

The threat of a military takeover by a reformist high command exasperated with the far right has loomed behind the talks ever since the March election of the constituent assembly. ARENA came close to scuttling the agreement on Magana, reached last week under heavy military pressure, by arguing to National Conciliation deputies that the Army and the United States were bluffing and that junior officers would support the right if it resisted.

In the end, however, the Army convinced the National Conciliation swing vote otherwise. The final vote in the 60-member body was 36 for Magana, 17 for ARENA's candidate, Hugo Barrera, and 7 abstentions. The abstentions included two National Conciliation deputies and the three minor party delegates plus Barrera and ARENA chief Roberto D'Aubuisson, president of the assembly.

"They agreed in order to combat the guerrillas and to avoid a coup d'etat attempt by officers of the second and third rank," said a member of the ARENA party directorate. "The guerrillas are out there dying of laughter watching all this. It has been a disgrace."

The Christian Democrats were jubilant. "I hope we can now enter on the road of consensus where we all work together for the good of the country," said their assembly leader, Julio Rey Prendes. He drew the loudest protest howls of the day from the galleries when he reminded the assembly that if his party had won the elections, it would have named current junta president Jose Napoleon Duarte to be provisional leader.

Duarte will remain in office as head of the current four-member military-civilian junta until Magana is inaugurated, probably sometime next week.

Magana has been president of a semi-public mortgage bank for 17 years and has been closely allied with the armed forces. A diffident, amiable man, Magana is a respected tax expert and a former university professor of economics. He served as undersecretary of the treasury in the 1960s.

While the Army and the Christian Democratic Party backed Magana as a political independent, ARENA politicians opposed him as too subject to the Army and too leftist. They argued that four rightist groups together won a 60 percent majority of all valid votes last month and had therefore won the right to name the president without any participation by the Christian Democrats.

But the Christian Democrats, with U.S. and Army backing, said they were entitled to participation in the government in proportion to their 40 percent of the vote. To exclude their moderating influence from the government in a nation where official observation of human rights is already suspect would have been virtually certain to alienate the U.S. Congress that controls the flow of aid, the Christian Democrats argued.

U.S. Ambassador Deane R. Hinton agreed, telling the politicians whenever he had the chance that Congress would act and that the Reagan administration would be powerless to stop an aid cutoff.

The Christian Democrats initially proposed a three-member ruling junta, an idea that was rejected by the other two parties. The compromise finally worked out, under U.S. and Army urgings, allowed each party to name its own vice president, with the division of labor among them to be determined later.

The major question now is how much real power Magana will have in the provisional government. D'Aubuisson is president of the constituent assembly, which must write a constitution and pass interim laws. ARENA and the National Conciliation Party, which joined ranks in the assembly to elect D'Aubuisson, control the 60-member body.

Magana has said in interviews that he plans to be "a coordinator, somebody that's going to try to make things work," more than a counterweight to the right. His main concern, he said, will be overseeing the rewriting of the constitution and the preparation for full presidential elections "within the next two years."

His backers, however, say his mild manner is deceptive. "He's a fox," said a former government co-worker. "He'll be very strong in the job."

The agreement worked out among the parties gives Magana the right to name his three most important ministers, those of defense, treasury and interior, but the assembly this week voted itself the right to ratif those choices.