At least 51 Nicaraguan soldiers, 32 of them counterinsurgency forces, were killed in a rebel attack last Friday on this town and in an ambush of soldiers being sent as reinforcements for the beleaguered local garrison, according to local military and civilian personnel.

While officials in Managua have denied that losses were so high, local officials and residents said rebels of the Nicaraguan Democratic Force entered this town of some 4,500 residents just before dawn and besieged the militia headquarters, killing eight Sandinista militiamen and capturing at least another 12.

The rebels, known here as counterrevolutionaries or contras, then ambushed a truckload of counterinsurgency troops as it passed down the only road leading into town and killed 32 soldiers, the military and civilian personnel said. Regional Army officials had told journalists that only five soldiers had died in that ambush.

The attack, 100 miles east of Managua, was the second major assault by the U.S.-backed rebels last week. The earlier attack took place Thursday at La Trinidad in the north of the country.

Cuapa is well known because in 1980 a peasant reported experiencing a vision of the Blessed Virgin Mary near here and a shrine built outside the town attracts many Catholic pilgrims.

"The shooting lasted about two hours," said Gustavo Henriquez, who said he lived across the street from the militia command post. "Everyone around was face down on the floor the whole time."

Rebels had entered La Trinidad, 80 miles north of Managua, after damaging with explosives two bridges on the all-important Pan American Highway and ambushing a truckload of Sandinista militia nearby, killing 29 men.

The attack in Cuapa is said to be the most serious attack ever by the rebels in Chontales province, which is prime cattle country.

According to residents, the rebels ordered townspeople out of their houses, made them report to the central plaza of the town and conducted a brief public meeting, during which they condemned the Sandinista government.

"They said they were going to march to Managua," said Luis Torres, 28, a clerk at a government food dispensary. "They said they represented Christianity and democracy."

During that meeting, according to residents, some of the townspeople spoke in behalf of a local official who had been captured by the rebels, Olman Martinez, 40, the locally elected delegate of the presidency.

"We told them he had done some good things for the town," said Ervin Rubina, 29.

Martinez, who was released the next day, told journalists Tuesday that he had been marched off with some 12 other captives, all Sandinista militiamen and officials, as the rebels withdrew from the town about three hours after entering it.

"Then I was marched off in one direction and everyone else in the other direction," said Martinez. "A few minutes later I heard some shots and I figured they had killed them."

Local authorities said they found the bodies of 11 Sandinistas thrown into a gorge near the town the next day. They said one woman Sandinista was still missing.

Martinez added that the rebels had been carting eight dead of their own. He said the contras indicated that he owed them his life and that they would be back to collect on that debt and expected him to cooperate.

"They said, 'You are a seed we are planting which we will harvest later,' " said Martinez.