When mutinous soldiers seized power in the West African nation of Burkina Faso early last year, the president of neighboring Niger struck a dismissive note.

“This is nonsense,” President Mohamed Bazoum told two Western diplomats who sat in his office as news of the coup came through. How cynical was it, Mr. Bazoum remarked, that the soldiers responsible for securing Burkina Faso had overthrown the government in the name of restoring security.

Now Mr. Bazoum faces the same fate. Deposed last month by soldiers who accused him of failing to protect Niger against Islamist groups, he is still a hostage in his own home three weeks on, running out of food and deprived of water and electricity.

Mr. Bazoum’s Western and African allies are trying to negotiate his release, and West African army chiefs on Friday were set to finalize a possible military intervention in Niger. But their hopes of restoring him to power are rapidly fading.

Mr. Bazoum was lauded in foreign capitals for his democratic credentials and tough stand against Islamist militants. But the coup and its aftermath have revealed his critics at home: the activists banned from protesting, the conservatives who objected to his advocacy of girls’ education, and the powerful generals he sought to sideline.

“Bazoum was on an island surrounded by alligators,” said Rahmane Idrissa, a Nigerien political scientist. “He had been haunted by the possibility of a coup.”

Niger is now in the hands of Gen. Abdourahmane Tchiani — the leader of the military unit that turned against the president it was supposed to protect.

It is a brutal fall for Mr. Bazoum, 63, a one-time high school teacher who served as foreign minister and interior minister before being elected in 2021 as president. He played a key role in turning Niger into a favored Western ally, even as critics cast doubt on the legitimacy of his victory and frustration mounted over policies that many saw as tailored to please Western partners.

Niger is one of the world’s poorest countries, and the broader Sahel region has become the global epicenter of Islamist militancy, despite a decade of Western-led military operations. For security partners like the United States and France, Mr. Bazoum offered the hope of regional stabilization. The military takeover has left them without a crucial ally.

When soldiers surrounded Mr. Bazoum’s residence in the capital, Niamey, on July 26, he fled into a safe room with his wife and son and called Western allies, alerting them to his plight, but also offering assurances. Mr. Bazoum believed that friendly army units would come to his rescue by nightfall and that he would return to power unscathed, said a former U.S. official who spoke with him that day, and described the conversation on the condition of anonymity to avoid alienating his contacts in the Nigerien military.

The cavalry never arrived. Western-trained Special Forces close to Mr. Bazoum had been dispatched to a remote desert area to prepare for Independence Day celebrations on Aug. 3, the former U.S. official said. The mutineers led by General Tchiani quickly established control.

Now, guards have barricaded Mr. Bazoum inside his home with extra locks, according to a member of his cabinet who requested anonymity to discuss Mr. Bazoum’s detention. He has refused to resign, but the junta in power has dissolved his government and named a new one.

After West African countries threatened military action to free Mr. Bazoum, the new military junta said it would kill him in the event of an intervention. It has also threatened to prosecute Mr. Bazoum for treason.

“The putschists have strengthened their grip on the state apparatus,” said Fahiraman Rodrigue Koné, a researcher with the Institute for Security Studies, in Mali. “Bazoum must be well aware that he lost power.”

At first, Mr. Bazoum did not see eye-to-eye with Washington. On his first trip to the United States as Niger’s foreign minister in 2011, he criticized the Western intervention in Libya, which, he predicted, would unleash a wave of instability in the region, said J. Peter Pham, a former Sahel envoy for the U.S. government, who hosted Mr. Bazoum during the trip.

Mr. Bazoum warned Obama administration officials that the military action in Libya was going to “throw all sorts of actors to the winds,” and that the United States needed to “help Niger before that sandstorm hits us,” Mr. Pham said.

The message was “not well-received,” Mr. Pham added. “The administration was very defensive and didn’t want to talk about collateral damage.”

Mr. Bazoum’s prophesy was partly borne out, as Islamist militants expanded across the region using weapons that, in some cases, spilled out of chaotic Libya. France deployed thousands of troops in the Sahel, and the United States set up major drone bases in Niger as part of a growing counterterrorism effort.

Mr. Bazoum, then interior minister, became the public face of that relationship in Niger. “All I know is they’re American,” he told The Times in 2018 about a newly established C.I.A. drone base in the country’s northeastern desert.

As president, Mr. Bazoum championed causes that also endeared him to Western partners. He promised to build 100 boarding schools across the country to educate girls and reduce early marriages. His goal, he said, was to bring down Niger’s birthrate of seven children per woman — the world’s highest.

Backed by Western military aid programs worth hundreds of millions of dollars, Mr. Bazoum ramped up a drive against groups tied to Al Qaeda and the Islamic State. He also promoted using dialogue to persuade extremists to lay down their arms in exchange for professional training and economic development.

Even as Mr. Bazoum partnered with the West, he courted other foreign powers seeking to build their influence in Africa. He purchased armed drones from Turkey and advanced the construction of a China-backed oil pipeline that could become a major economic boon to Niger.

His approach seemed to be bearing fruit this year when, over the first six months, attacks on civilians in Niger dropped by 39 percent, according to the Armed Conflict Location & Event Data Project. In neighboring Mali and Burkina Faso, where soldiers seized power in 2021 and 2022, violence soared.

But Mr. Bazoum’s enthusiastic support from the West was not mirrored at home, where critics asserted that his election had been rigged. (Observers from the African Union judged it a fair vote, and Niger’s top court later confirmed the result.)

He tried to move away from the lingering influence of his predecessor, Mahamadou Issoufou, visiting victims of terrorist attacks while his administration prosecuted dozens of officials on corruption, an approach seen as both more caring and tougher than Mr. Issoufou’s.

But despite his teaching skills, Mr. Bazoum failed to adequately explain ideas, like his support for educating girl, to the public, observers said.

“He imposed his methods with a paternalistic approach, telling Nigeriens what would be good for them,” said Mr. Idrissa, who had Mr. Bazoum as a high school teacher and is now a senior researcher with the African Studies Center of Leiden University in the Netherlands.

As a growing number of Nigeriens grew resentful of Niger’s military ties to France, the former colonial power, Mr. Bazoum’s administration showed little tolerance for protest.

There was also increasing tension with his military. Some officers resented Mr. Bazoum for releasing Islamist insurgents who had killed their men, analysts said. And some generals pressed for a closer partnership with the junta in neighboring Mali, who had brought in the Kremlin-backed Wagner paramilitary group. Mr. Bazoum wanted no part of that.

Mr. Bazoum intended to fire Gen. Tchiani, head of the 700-strong Presidential Guard, at a cabinet meeting that was scheduled for July 27, according to two advisers to the president and a Nigerien intelligence officer who spoke on condition of anonymity to discuss Mr. Bazoum’s plans about Gen. Tchiani.

Instead, a day earlier, on July 26, Gen. Tchiani struck first.

Now, few Nigeriens dare voice public support for Mr. Bazoum. The junta has arrested many of his allies, while others have fled abroad. Thousands of mostly young Nigeriens have twice filled a sports stadium to support the military.

One day in the capital this month, a woman in a taxi could be seen shouting out the window, “We need Bazoum!”

Dozens of young junta supporters overturned the taxi and attacked the woman. She was rescued from the crowds by soldiers standing nearby.