NAIROBI — Just hours after soldiers in the oil-rich Central African country of Gabon seized control of the national broadcaster early Monday morning and claimed they had deposed the country’s absent leader to “restore democracy,” a spokesman for the government said the coup attempt by the “mutineers” and “jokers” had been thwarted.

Reports from news agencies said the coup bid was accompanied by scattered gunfire in the capital, Libreville, and videos posted on social media showed armored vehicles speeding through the streets as helicopters circled overhead.

Reuters reported that two of the officers suspected in the plot were killed and seven others were arrested.

After suffering an apparent stroke in October, Gabon’s president, Ali Bongo, traveled for treatment to Saudi Arabia and then to Morocco, where he has been recovering. In his first public statement since falling ill, he issued a New Year’s address from the Moroccan capital, Rabat, acknowledging that he had been “through a difficult period” and promising to return soon.

The leaders of the coup attempt read out a statement on state radio in the pre-dawn hours denouncing Bongo. Lt. Kelly Ondo Obiang, the leader of the self-declared Patriotic Youth Movement of the Defense and Security Forces of Gabon, said Bongo’s New Year’s address had “reinforced doubts about the president’s ability to continue to carry out the responsibilities of his office.”

“If you are eating, stop; if you are having a drink, stop; if you are sleeping, wake up. Wake up your neighbors . . . Rise up as one and take control of the street,” Obiang said over the radio.

By midmorning on Monday, however, it appeared that the coup attempt had failed.

“Calm has returned. The situation is under control,” government spokesman Guy-Bertrand Mapangou said, adding that the gunfire earlier was merely intended to control a crowd.

An official close to the president’s office told Radio France International that the strategic points in the country are under the government’s control, including the radio station. The army is seeking to resolve the situation without violence, the official said.

Internet service reportedly had been cut in the capital, and many areas were without electricity. But reports from news agencies indicated that those services were quickly returning.

In his speech on state radio, Obiang said the army high command has failed in its mission to defend the country, and he called on rank-and-file soldiers to “take control of all means of transport, army bases and security posts, armories, and airports.”

He also called for the formation of a Council of National Restoration and invited members of civil society and opposition parties, as well as a former Republican Guard commander, to meet at the country’s parliament.

Gabon lies on Africa’s Atlantic coast, bordered by Congo Republic, Equatorial Guinea and Cameroon. It was formerly ruled by France, which maintains a military presence there. Last week, the United States deployed 80 troops to Gabon to evacuate Americans from nearby Congo in the event that a disputed election there turns violent.

Bongo came to power in 2009 after the death of his father, Omar Bongo, who ruled the country for 42 years. His narrowreelection in 2016 was marred by violence and accusations of fraud.

Bongo’s half brother, Frederic Bongo, is in charge of Gabon’s intelligence service and is closely aligned with the military.

Paul Schemm in Addis Ababa, Ethi­o­pia, contributed to this report.