(Reuters) - Nigeria's military repelled an attack by suspected Boko Haram militants on Borno state capital Maiduguri in the northeast, security sources said on Sunday, but the insurgents captured another Borno town.

The assault on Maiduguri, with a population of around two million, began just after midnight. Sources at two hospitals said at least eight people had died and 27, mostly civilians, had been injured. A raid on the town of Monguno, 140 km (80 miles) north, began later in the morning and was under militant control by the late afternoon.

The army's inability to quash the Sunni jihadist group is a major headache for President Goodluck Jonathan, who is seeking re-election in February and who visited the state capital on Saturday as part of his campaign. Opposition candidate Muhammadu Buhari had been due to arrive on Monday.

The elections are expected to be the most hotly contested since the end of military rule in 1999 with many fearing violence in the aftermath.

U.S. Secretary of State John Kerry arrived in the commercial capital Lagos on Sunday to urge the candidates and their supporters to respect the election outcome, underscoring U.S. concerns that post-poll violence could destabilize the country and undermine the fight against Boko Haram.

The five-year insurgency, which aims to carve out an Islamic state in Africa's most populous country, has seen thousands killed.

At around 9 a.m. (0300 ET) on Sunday, a Reuters witness in Maiduguri said shelling could be heard and that military helicopters were circling the city, but by the early afternoon calm had returned.

Scores of militants and soldiers were killed in the clash, a military source and a civilian joint task force leader (JTF) said.

"We're still combing the bush for their corpses, we pursued the Boko Haram men until Auno, 12 km outside the city...some fled towards Mainok," said the civilian JTF leader who declined to be named for security reasons.

Nigeria's defense headquarters tweeted on Sunday morning that coordinated land and air operations were being used to repel the attacks and a curfew had been imposed on Maiduguri.

The militants began the attack at the edge of the city in the Njimtilo area and tried to take the airport which services the air force as well as civilians, security sources said. Boko Haram last attempted to take Maiduguri from the same area in December 2013.

Along with much of Borno, the militants control some areas of neighboring Adamawa and Yobe states, and recently took control of the town and a multi-national army base at Baga by Lake Chad.

The government said 150 people had been killed in that attack but local officials say the figure is far higher and some have put it as high as 2,000.

In Monguno, some 50 km (30 miles) from Baga, security sources said that the military was being overwhelmed by Boko Haram's firepower. Houses in the town were also being set ablaze.

"Boko Haram has more power than us and are shelling the town ... our colleagues are fleeing," a soldier in Maiduguri said after speaking to friends fighting the insurgents in Monguno.

Residents of Monguno have had to flee the town.

"We saw the mobile police and soldiers also running away, some of them telling us to find our way out as they have been overpowered," Madu Bakur, a Monguno resident told Reuters.

(Additional reporting by Ardo Abdullah in Bauchi; Additional reporting and writing by Julia Payne in Abuja; Editing by Catherine Evans and Stephen Powell)