Boko Haram jihadists were in control of a town in northeast Nigeria on Saturday after sacking a military base, in the latest attack that raises questions about claims they are weakened to the point of defeat.
Local officials and security sources said scores of fighters believed to be loyal to a Boko Haram faction backed by the Islamic State group overran troops in Gudumbali.
At least eight civilians were believed to have been killed, while thousands of others fled to neighbouring towns.
Gudumbali, in the Guzamala area of Borno state, is Boko Haram's first major seizure in two years and comes after a series of recent attacks on troops.
The authorities and the military have been encouraging people displaced by violence in the long-running conflict to return to Guzamala, insisting it is safe to do so.
But aid agencies say minimum levels of basic services, including shelter, civilian infrastructure and security are still lacking.
President Muhammadu Buhari, a former army general, was elected in 2015 on a promise to defeat Boko Haram and is seeking a second term of office at polls in February.
The Gudumbali attack will again raise questions about his claims to have "technically defeated" the group and that Borno state was now in a "post-conflict stabilisation phase".
An official of the Guzamala local government area, of which Gudumbali is the headquarters, confirmed troops had been pushed out of the town and Boko Haram was in "full control".
A military source in the Borno state capital, Maiduguri, said the attack began at about 7:50 pm (1850 GMT) on Friday and lasted until the early hours of Saturday, "when troops were forced to withdraw".
Local civilian militia member Musa Ari said: "So far eight civilians, who were errand boys for troops, were believed to have been killed in the attack."
But "most civilians were spared because the attack was targeted at the military base", he added.
The IS-backed faction -- known as Islamic State West Africa Province (ISWAP) -- has vowed to hit only "hard" military or government targets.
It is reportedly trying to get the support of local populations in the Muslim-majority region.
Ari said soldiers and residents fled Gudumbali to Damasak, some 80 kilometres (50 miles) away, on the border with Niger.
Others escaped south towards Gajiram, where nine soldiers were killed in a similar attack in June.
Nigerian Army spokesman Brigadier General Texas Chukwu said he was "not aware" of the latest attack.
ISWAP fighters led by Abu Mus'ab al-Barnawi were last month blamed for an attack in Zari village, just 50 kilometres away from Gudumbali, which killed 48 soldiers.
In July, dozens of troops were said to have been killed, wounded or missing in a similar attack on a base in Jilli village, across the border in Yobe state.
Yan St-Pierre, head of the Modern Security Consulting Group, said the Gudumbali attack was "another demonstration of ISWAP's increasing capabilities and level of strength".
"They've been able in recent months to attack larger, more important targets with increasing frequency and success," the counter-terrorism specialist told AFP.
"It is likely to get worse because ISWAP is not only adapting to changing circumstances but benefiting from the changing dynamics in the Sahel as well."
The Nigerian military regularly trumpets successes against Boko Haram and has strongly condemned any reports of significant troop losses.
But there are indications of disquiet in the ranks, mirroring the situation four years ago when Boko Haram ran rampant across the northeast.
Then, under-equipped troops in some instances refused to deploy.
A military counter-insurgency has since driven out Boko Haram from captured territory, including Gudumbali, which was captured in 2014.
But aid agencies providing food, shelter and healthcare to 1.8 million displaced by the conflict, say much of the hard-to-reach countryside remains in Boko Haram control.
Last month, hundreds of soldiers protested at the airport in Maiduguri, for several hours, shooting into the air and disrupting flights.
They complained about being battle weary and needing home leave after sometimes years on the frontlines.
St Pierre said Nigeria's military needed to break the cycle by acknowledging its tactics against the insurgents were not working and by addressing low morale.
If it does not, "it will simply never be in a position to defeat them", he added.