(Adds ISIL statement)
BAGHDAD, April 3 (Reuters) - Militants attacked the headquarters of an Iraqi army battalion south of Baghdad overnight, killing at least 12 soldiers and bringing their fight against the government closer to the capital.
Police and medical sources said fighting began late on Wednesday when the assailants fired mortar rounds at the battalion's headquarters in Yousifiya, around 15 to 20 km (9 to 12 miles) southwest of central Baghdad.
Roads leading to the area were closed off on Thursday as clashes continued and reinforcements were dispatched there. In total, around 40 gunmen were either killed, wounded or detained, the sources said.
The identity of the attackers was not clear, but Sunni Islamist insurgents are regaining ground in Iraq and have overrun several towns and cities since the start of the year, including Falluja, around 70 km from Baghdad.
The Iraqi army has been deployed around Falluja since the start of the year. It is in a standoff with Sunni militants inside the city, including the Islamic State in Iraq and the Levant (ISIL), which is also active in neighbouring Syria.
The conflict has raised doubts parliamentary elections can be held nationwide later this month as intended.
In a statement on Thursday, ISIL urged Sunnis to reject democracy and rally behind the "holy warriors" against Iraq's Shi'ite-led government. It said the group made good on a pledge to retake territory lost after the U.S.-led invasion in 2003.
"We swore to return to the areas from which we withdrew and more ... today we are stronger than yesterday and our enemy is weak and collapsing," ISIL spokesman Abu Mohammed al-Adnani in an audio recording.
A spokesman for the Interior Ministry said only one soldier had been killed in the attack on the battalion headquarters and put the death toll among the militants at 40. He dismissed the prospect they might make inroads into the capital.
"They are not an army to be able to encroach on Baghdad: it is not possible for them to do that at all," said spokesman Brigadier General Saad Maan "They are trying to find a foothold".
Maan said militants were taking cover in the orchards and farms surrounding Baghdad because they had nowhere else to go, and it was harder for the security forces to mobilise in such terrain.
Of the 592 Iraqis killed in acts of violence in March, 108 were members of the security forces, according to figures published by the United Nations. (Reporting by Raheem Salman and Kareem Raheem; Writing by Isabel Coles; Editing by Larry King)