* Thousands flee Bama as fighting rages
* Heavy casualties both for Boko Haram and military
* Botched air strike kills several Nigerian troops
* Fears Maiduguri could be next target (Adds UNHCR on refugee numbers, curfew)
By Lanre Ola
MAIDUGURI, Nigeria, Sept 2 (Reuters) - Islamist Boko Haram insurgents have overrun much of a northeastern Nigerian town after hours of fighting that killed scores and displaced thousands of residents, security sources said on Tuesday.
The Islamists launched an attack on the town of Bama, 70 km (45 miles) from the Borno state capital of Maiduguri, on Monday. They were initially repelled but came back in greater numbers overnight, the sources and witnesses said.
Nigeria's defence spokesman did not respond to a request for comment. The sources said there were heavy casualties on both sides and one said at least 5,000 people fled the town.
In a bungled air strike, several Nigerian troops were killed at the Bama armoury by a fighter jet targeting the insurgents, a soldier on the ground told Reuters.
Two months after Islamist militants in Iraq and Syria declared the area they seized an Islamic caliphate, Boko Haram has also for the first time explicitly laid claim to territory it says it controls in parts of northeast Nigeria.
They captured the remote hilly farming town of Gwoza, along the Cameroon border, during fighting last month. The group's leader Abubakar Shekau in a video declared it a "Muslim territory" that would be ruled by strict Islamic law.
Shekau's forces have killed thousands and displaced hundreds of thousands since launching an uprising in 2009 to revive a medieval Islamic caliphate in religiously mixed Nigeria. They are the biggest security threat to Africa's top oil producer.
"When we started hearing gunshots, everybody was confused. There was firing from different directions. We just ran to the outskirts of town," Bukar Auwalu, a trader who fled with his wife, three children and brother, told Reuters by phone.
"There were military helicopters and a fighter jet. We slept in the bush on the outskirts of town. We can't go back."
Adrian Edwards, spokesman for the United Nations High Commissioner for Refugees (UNHCR), said 9,000 people fleeing violence in Nigeria's northeast had arrived in Cameroon's Far North region in the past 10 days.
"Even upon arrival in Cameroon, they are not necessarily out of harm's way. On Sunday, insurgents attacked Kerawa town inside Cameroon, forcing refugees and some local residents to flee further inland," he said in a briefing note from Geneva.
Another 2,000 had crossed into Niger, which is already hosting some 50,000 refugees of fighting since May 2013. Some 645,000 people are internally displaced in Nigeria, she said.
Because of Bama's proximity to Maiduguri, a large metropolis and home of a major army base, security officials are worried there is now little to keep Boko Haram from gaining access to a key city that was also the birthplace of their movement.
The military has extended Maiduguri's nightly curfew to 7 p.m. (1800 GMT) until 6 a.m. - it previously started at 10 p.m. - to fight "infiltration into the Maiduguri metropolis by insurgents," Nigerian defence headquarters announced on its Twitter account on Tuesday.
Boko Haram attracted the international spotlight on April 14, when its fighters kidnapped more than 200 girls from a school in the northeastern village of Chibok in April. They remain in captivity.
Their tactics have started to resemble those of Ugandan warlord Joseph Kony and his Lord's Resistance Army: kidnapping boys and forcing them to fight while taking girls as "wives".
A military offensive launched over a year ago to crush them has merely made the rebels more brutal.
The apparent powerlessness of the military to protect civilians or prevent the militants' raids has triggered much criticism of President Goodluck Jonathan's administration, although it argues counter-insurgency is something new that the government is having to learn how to fight.
A soldier involved in the Bama fighting, who declined to be named, said the insurgents had targeted the Bama armoury with heavy weapons including tanks. As troops tried to repel the attack, they called in air reinforcements.
But by the time the fighter jet arrived, they had mostly lost the battle for this location. The jet then bombed the area but accidentally killed everyone there, both Nigerian troops and insurgents, the soldier said.
"The situation is bad. We lost so many of our men," he said.
Farmer Ibrahim Malu said hundreds had fled the town. He had gone out to his farm early in the morning when gunfire and loud explosions erupted. He ran back home, but by the time he got there his wife and children had already fled.
"I still don't even know where they could be," he told Reuters by telephone. "Two soldiers fled with me. One of them didn't even have any shoes." (Reporting by Lanre Ola; Additional reporting by Tim Cocks in Lagos and Emma Farge in Dakar; Writing by Tim Cocks; Editing by Alison Williams, Dominic Evans and Tom Heneghan)