Leftist guerillas ended their siege of Nicaragua's main government building yesterday, flying to Panama with political prisoners released in response to their demands.

The 40 to 50 guerillas released most of their estimated 1,500 hostages, but were accompanied to Panama by a number of high-ranking officials who volunteered to guarantee the safe departure of the members of the Sandinista National Liberation Front.

There were conflicting reports on the number of political prisoners released and the amount of ransom paid by the government of President Anastasio Somoza. One source said 83 prisoners were released while others said the number was unknown.

One government official said the guerrillas were given $71,000, while other said $500,000. They had originally demanded $10 million.

One thing was certain, however. Significant members of Nicaraguans are willing to make their antigovernment feelings known.

Thousands lined the route to the airport cheering the guerrillas as they drove past. The crowds chanted "Down with Somoza," and "Somoza to the gallows."

When the two planes carrying guerrillas and hostages took off, jubilant Nicaraguans broke through security lines at the Managua airport and cheered.

"It's fantast - ic. It's tremendous. It's a triumph for the people," said one young woman in the airport crowd. Riot police turned water cannon on the demonstrators at one gate when they threatened to swam onto the runway.

President Somoza said at a news conference in Managua that he allowed the guerrillas to leave for humanitarian reasons.

"I hope Nicaraguans understand why we took this action . . . to save human lives," the president said.

Some Nicaraguan legislators who had been held hostage, including Somoza's counsin Luis Pallais Debayle and two or three others, were freed at the Managua airport before the planes left for Panama.

Reporters at the airport in Panama estimated that about 100 persons - guerrillas, hostages and freed political prisoners - got off the two planes. Among the passengers were three Roman Catholic prelates who had negotiated with the guerrillas since they shot their way into the legislative building Tuesday.

One person was taken from one of the planes on a stretcher and rushed to a nearby hospital. There had been reports that one of the guerrillas was wounded in the attack on the building. The rest of the passengers were taken to an airport building as Panamanian national guardsmen ringed the airport. After processing, the Nicaraguans were placed on two buses and driven to an undisclosed destination.

Reporters estimated that about 60 persons left on the buses, and other group was believed to have remained in the terminal building at Tocumen International Airport.

The guerrillas are trying to overthrow Somoza and install a Marxist government in Nicaragua.

The leader of the guerrillas, identified only as "Commander Zero," told reporters his men and the freed prisoners decided to stay in Panama because it is close to Nicaragua and they could carry on their battle against Somoza from here.

"Zero," wearing green battle fatigues and a black beret, rejected suggestions that the guerrillas are financed by Cuba.

"We didn't need help from anyone," said "Zero," in a hoarse voice. "We're intelligent, we're capable, we're revolutionaries."

A reporter at the National Palace, the capitol building in Managua, said the guerrillas left for the airport with at least eight hostages, including three Roman Catholic bishops and the Panamanian and Costa Rican ambassadors to Nicaragua, who volunteered to accompany them out of the country, and three or four members of the Nicaraguan parliament.

"Commander Zero" told a Panamanian reporter in a telephone interview from the capitol building that the operation was "defintely a victory," and claimed Somoza's downfall was inevitable.

"What I cannot tell you is whether that will happen tomorrow or next year," he said. "But he will fall."

The iron-fisted Somoza government has been beset by popular unrest since earlier this year, when a journalist and outspoken foe of Somoza was murdered.

Guerrillas of the Sandinista National Liberation Front took more than 1,500 persons captive Tuesday when they stormed the white-columned four-story palace, where a meeting of the Chamber of Deputies was under way. The guerrillas, disguised as members of the National Guard, Nicaragua's army, shot their way into the building in downtown Managua, killing eight persons, most of them guardsmen, and wounding 15 others.

About 300 persons, mostly women and children, were freed by the guerrillas as negotiations dragged on Wednesday.

It was only after the guerrillas left for the airport and the last throng of hostages emerged from the palace that the size of the captive group became apparent.

Two of the captive legislators put the number of persons released yesterday at 1.214. The haggard hostages emerged in small groups as troops poured through the palace. They were led to waiting buses and driven to an undisclosed location.

The guerrillas had threatened to execute their hostages, who included many of Nicaragua's 70 congressmen and a number of government officials.

The Sandinistas take their name from Augusto Cesar Sandino, a guerrilla leader of the 1920s and 1930s who died fighting the U.S. Marines who occupied Nicaragua and put Somoza's father, also named Anastasio, in power in 1937. The family has ruled the country ever since and without serious challenge to its power until this year.

The guerrillas have been launching small-scale attacks on Somoza's troops for a decade. Four years ago they kidnaped more than 40 persons attending a party. The government gave in to their demand for $1 million, a plane to Cuba and the release of political prisoners.

Somoza's grip was not seriously threatened until after the assassination Jan. 10 of one of his most outspoken foes, publisher Pedro Josquin Chamorro. The killing touched off riots, strikes and other demonstrations against the government, calls for Somoza's resignation and continuous unrest. Somoza, 52, refused to step down and said he would stay in office until his term expires in 1961.