Hours after soldiers seized power in the West African nation of Niger, the country’s ousted president sounded a defiant note on Thursday morning, vowing to protect his “hard-won” democratic gains, even as he was being held by his own guards.

The president, Mohamed Bazoum, appeared to be still in detention at the presidential palace in the capital, Niamey, where his guards turned on him early Wednesday, prompting a crisis in the vast, largely desert nation twice the size of France.

“The hard-won gains will be safeguarded,” Mr. Bazoum said in a message on social media. “All Nigeriens who love democracy and freedom would want this.”

The country’s foreign minister adopted a similar tone, telling a French television station that Mr. Bazoum remained the sole “legitimate power” in Niger, and that the military was not united in the attempted coup.

The statements suggested that the coup declared by a group of soldiers on national television on Wednesday night was incomplete, that Niger’s beleaguered civilian leaders were standing their ground, and that they held out hopes that the ouster might somehow be reversed.

“We ask all the fractious soldiers to return to their ranks,” the foreign minister, Hassoumi Massoudou, told France24 television. “Everything can be achieved through dialogue.”

But the mutinous officers, who call themselves the National Council for the Safeguarding of the Country, seemed determined to push ahead. On television, they announced that Niger’s borders would be closed, its government suspended and a nighttime curfew imposed.

Early Thursday, a sandstorm rolled through Niamey, where many businesses remained closed, adding to the sense of uncertainty.

Although detained, Mr. Bazoum has remained in contact throughout the crisis with regional and Western leaders, including Secretary of State Antony J. Blinken, who called Mr. Bazoum to offer his “unconditional support.”

At least 1,100 American troops are stationed in Niger, one of the few countries in the arid Sahel region that remains a staunch Western ally in the fight against the Islamist militant groups that are spreading chaos across the region.

If the coup succeeds, it will be West Africa’s sixth military takeover in less than three years, following in the footsteps of Mali, Guinea and Burkina Faso. It would also be a serious blow to democracy efforts in a problem-plagued region that is regaining its unwanted reputation as the “coup belt” of Africa.

Surging Islamist militancy, the ravages of climate change and the failure of fragile states to provide much for their exploding, youthful populations are among the factors that left those countries vulnerable to coups. Still, Niger hoped to be different.

Mr. Bazoum was freely elected two years ago in the country’s first peaceful democratic transfer of power since independence from France in 1960. He allied closely with the West to combat the militant groups that sprang up the far reaches of Niger’s vast deserts, often spilling over from Mali and northern Nigeria.

As Mali and Burkina Faso turned to Russia’s Wagner private military company for help to fight the militants, Mr. Bazoum stuck with France and the United States. As well as troops, the Pentagon has two drone bases in Niger that have been used to carry out airstrikes in Libya.

When the last French troops departed Mali this year, after a collapse in relations between Paris and Mali’s ruling junta, some of them redeployed to bases in Niger.

The president of neighboring Benin, Patrice Talon, said he was flying to Niamey on Thursday in an effort to mediate the crisis.