The Saturday attack on Haditha, located northwest of Baghdad in Anbar province on the road linking militant - held western areas and the provincial capital, began with mortar fire, police said.
Gunmen travelling in vehicles, including some captured from security forces, then attacked from two sides but were kept from entering the town in fighting that left 13 militants and four police dead, officers and a doctor said.
While there have been previous attacks on Haditha, they have not been on this scale. Capture of the dam by the militants would raise the prospect of it being used to flood towns and villages downstream.
Militants earlier this year caused major flooding by releasing water from another dam in Anbar province.
In Diyala province, meanwhile, security forces and civilian volunteers Saturday launched a push to retake militant - held areas north of Muqdadiyah, a town on a main road to provincial capital Baquba, a police captain said.
In the town of Jalawla, also in Diyala, Kurdish peshmerga fighters began a major operation to expel militants from areas they hold, a senior Kurdish officer said.
Major General Hussein Mansur said Kurdish forces were using tanks and artillery in the battle, and had succeeded in retaking some territory from the militants.