Niger thwarted an "attempted coup" on Wednesday, the government said, two days before the historic inauguration of Mohamed Bazoum as president of the unstable Sahel nation.

Residents in the capital Niamey earlier reported hearing gunfire in the middle of the night in the district where the presidency is located.

A government statement said "an attempted coup" had been blocked, condemning what it called a "cowardly and regressive act which sought to threaten democracy and the state of law."

An investigation has been launched and "several people connected with this attempted coup have been detained and others are being actively sought," it said, without giving further details.

The alleged leader of the coup is an air force officer in charge of security of Niamey's military air base and is still being "actively sought", a source within Niger's security services told AFP.

"The situation is totally under control," the government statement said, calling on the public to "carry on with normal life."

The alleged coup took place two days before the inauguration of Niger's newly-elected president, Mohamed Bazoum
The alleged coup took place two days before the inauguration of Niger's newly-elected president, Mohamed Bazoum AFP / Issouf SANOGO

A security source, speaking on condition of anonymity, said "there were some arrests among a few members of the army" who had been behind the coup bid.

"The Presidential Guard retaliated, preventing this group of soldiers from approaching the presidential palace," the source said.

A resident of Niamey's Plateau district, which includes the president's official residence and offices, told AFP: "It was around 3:00 am, we heard shots from heavy and light weapons and it lasted 15 minutes before stopping, followed by shots from light weapons."

"The gunfire lasted about 20 minutes," another resident said.

A third resident spoke of "intense shooting, with heavy and light weapons".

Online newspaper reported that calm had returned by around 4:00 am.

Niger's President Mahamadou Issoufou (center) attends a rally in support of the army's war against Boko Haram in Niamey, Feb. 17, 2015. REUTERS/Tagaza Djibo

In short video clips posted on social networks, only several seconds in length, sporadic bursts of gunfire could be heard in the pitch dark.

It was not immediately possible to verify the videos independently.

The presidential district was being patrolled by security forces, but in the rest of the city, life seemed to carry on as normal some 12 hours after the coup, an AFP journalist said.

UN Secretary General Antonio Guterres was monitoring the situation in Niger with "great concern", his spokesman Stephane Dujarric said.

"He urges all involved to desist from any form of incitement that threatens democratic consolidation and the stability of the country," he told reporters.

"He particularly calls on the armed forces to strictly abide by their constitutional obligations."

Nigerian President Muhammadu Buhari, who himself once took power in a coup, condemned the attempted putsch, saying he was "particularly concerned about the negative impact of coups on African stability, peace and progress".

The US embassy said it was suspending consular services until further notice and advised its employees not to come to work, and the French embassy similarly urged French nationals to stay at home.

The alleged coup comes ahead of Bazoum's inauguration on Friday -- the first elected transition in Niger's history since independence from France in 1960.

Numerous heads of state are expected to attend the ceremonies, according to the government.

Bazoum is a former interior minister and right-hand man of outgoing president Mahamadou Issoufou, who is voluntarily stepping down after two five-year terms, a move that has been warmly praised by France and others.

Bazoum's victory in the second round of elections on February 2 was confirmed by the constitutional court this month.

The runner-up, former president Mahamane Ousmane, contested the results, claiming that he was the real winner.

Ousmane had called for "peaceful marches" across the country, but authorities banned a planned opposition protest Wednesday in Niamey the day before.

Niger is the poorest country in the world, according to the benchmark of the UN's 189-nation Human Development Index (HDI).

It has suffered four coups in its history, most recently a February 2010 putsch which toppled then president Mamadou Tandja.

The Sahel country has also been struck by repeated jihadist attacks, from insurgents who have advanced from Mali in the west and Nigeria in the southeast.

More than 300 people have been killed in three attacks in the west since the start of the year.

In the most recent of these, 141 members of the Tuareg community were massacred on March 21 in Tahoua, a vast desert region abutting Mali.

Bazoum has ruled out any talks with the insurgents.

On March 8, Issoufou, the outgoing president, was awarded the coveted Ibrahim Prize -- Africa's top prize for leadership.