The mysterious decapitation of a prominent Panamanian adventurer and a leadership struggle in the military played major roles in the downfall last Saturday of Panama's first elected president in 16 years, according to reliable sources here.

To keep the story quiet, officers in the Panamanian Defense Force have warned local journalists that vigorous reporting or comment on the subject could endanger them, according to journalists and other sources.

One prominent columnist has gone into hiding after military intelligence agents showed up at his house, and U.S. Ambassador Everett Ellis Briggs has expressed concern to editors of the caustic opposition paper La Prensa that their comments could lead the military to close them down, journalists said.

These circumstances surrounding President Nicolas Ardito Barletta's resignation have brought to the surface the realities of military power here that were obscured during 11 months of a civilian rule that was endorsed by the United States.

In so doing, the end of Ardito Barletta's tenure has underlined the fragility of Reagan administration attempts to foster democratic institutions in the regions's countries where the seat of power is the barracks.

To dramatize its displeasure, the administration announced suspension of $5 million in aid three days after Vice President Eric Arturo Delvalle replaced Ardito Barletta. But a Panamanian official expressed confidence that U.S. relations with the Defense Force will remain close as long as American military bases here and the Panama Canal are unaffected.

"It doesn't matter who the president is," said a highly placed Panamanian political source. "What matters is what the Defense Force does."

The military commander, Gen. Manuel Antonio Noriega, had grown increasingly impatient with Ardito Barletta's performance in managing the troubled Panamanian economy, particularly a $3.8 billion debt. Delvalle charged in his inauguration speech that Ardito Barletta demonstrated lack of political leadership at a time of economic crisis, suggesting this was the only reason for his unceremonious ouster.

But the dissatisfaction that had been brewing for months boiled over only after the decapitated body of Dr. Hugo Spadafora was found just inside Costa Rica on Sept. 14, Panamanian and other sources pointed out.

Spadafora, a 45-year-old physician, was a public enemy of Noriega, having accused the Defense Force commander of drug trafficking and arms smuggling. In addition, he was well-known here as a romantic and adventurous figure from a prominent Panamanian family.

After traveling to Africa to participate in the war against Portuguese colonialism two decades ago, Spadafora served as public health minister under the late and still widely revered general Omar Torrijos. Later, with backing from Torrijos, he organized and led a Panamanian battalion that joined the Sandinistas in the fight against then-president Anastasio Somoza in Nicaragua.

Disenchanted with the Sandinistas after they took power in 1979, he joined Eden Pastora in a Costa Rica-based guerrilla movement trying to overthrow them. Spadafora eventually fell out with Pastora, however, and switched his allegiance last year to Miskito and Sumo Indian guerrillas, also headquartered in Costa Rica.

Finally, his family said, he decided two months ago to abandon the fight against Sandinista rule and return to Panama to engage in politics against Noriega. Some reports suggested he might have violent struggle in mind. But his brother Winston, a Panama City lawyer, said Spadafora intended to oppose the Panamanian military through peaceful political action.

In any case, many Panamanians began to suspect the Defense Force had him killed, despite Noriega's denials.

Costa Rican police investigators reported finding witnesses who said Defense Force troops arrested Spadafora just after he crossed into Panama on Sept. 13, the day before his body was found -- in a U.S. mail bag, according to the police account. An autopsy report said the symbol "F8" was carved on the mutilated and headless body. "F7" was the name of a SWAT team tied to the Defense Force that attacked antigovernment demonstrators during Ardito Barletta's campaign last year, Panamanian sources said.

Armed with these indications, the Spadafora family publicly accused Noriega and the Defense Force. The victim's father, Carmelo, 78, also a former minister, told reporters that Noriega was behind the killing and demanded the Legislative Assembly name an independent investigating commission.

Winston, the brother, began a fast eight days ago to dramatize the demand. His fast and the family's demands have been daily front-page news in La Prensa.

Ardito Barletta publicly refused to name a commission, saying the government judiciary already was investigating. A well-placed political source said, however, the president was considering a change of mind as he left Tuesday for the United Nations. Ardito Barletta has refused to meet with reporters since he was forced out.

As the Spadafora drama built up political tension, Noriega had to deal with his Defense Force of 12,000-15,000 men. The commander, who had been in Britain and France since early September, returned late Wednesday amid rumors that some officers were trying to unseat him in a barracks coup.

According to reliable sources, Col. Roberto Diaz Herrera, the chief of staff, had sought to organize an anti-Noriega movement but failed to gain enough support to make it work. Diaz Herrera is among a group of senior officers scheduled for retirement this month, the source said, and has been reluctant to face the end of his career.

Panamanian and foreign sources suggested some Defense Force officers also were upset at Noriega and Ardito Barletta for failing to halt the accusations about Spadafora's killing. One report said the officers had received word that Ardito Barletta was sounding out legislators about the possibility of an independent commission as demanded by the Spadafora family.

Noriega, eager to reassert his leadership, began meetings with officers and political leaders Thursday, the informants said, and then called Ardito Barletta at the United Nations and ordered him back to Panama. Noriega has refused to explain his actions.

Briggs, the U.S. ambassador who was with Ardito Barletta in New York, urged the president to stay put in hopes the crisis would work out through compromise, knowledgeable Panamanian sources said. The embassy has refused to comment on the accounts.

Ardito Barletta returned in the night, arriving here about 7:30 a.m. Friday and going into a conference with Noriega and other officers that ended more than 14 hours later with his resignation.