QARA TEPE, Iraq—Even as an al Qaeda-linked militant group celebrated a major victory in Western Iraq last month, militants from the same jihadist group launched another operation clear across the country.

In coordinated predawn attacks, gunmen blew up two bridges in a village outside the eastern town of Qara Tepe. They detonated a fuel tanker at a police base close to nearby Injana, shot 12 soldiers and incinerated their bodies. By afternoon, militants had attacked four other police and army checkpoints.

Instead of bolstering their ranks, some police and military checkpoints simply packed up and left. Lacking protection, hundreds of villagers fled their homes for larger towns.

"The security forces are weak, and they are putting the responsibility for their weakness on us," says Aziz Latif, a farmer who fled the village of New Sari Tepe after it was attacked on March 21. "They are not professional."

More than two years after the last U.S. troops left Iraq, as the country prepares for its first post-occupation parliamentary elections on Wednesday, its demoralized, underequipped military is losing the fight against Islamist militants, who are better armed, better trained, and better motivated, according to Iraqi and American generals, politicians and analysts.

"You can see how terrorism is eating our flesh. We're almost helpless," says Staff General Mohammed Khalaf Saied Al Dulaimi, commander of 12th division of the Iraqi army based in the northern city of Kirkuk. "We're facing a good, well-trained enemy. The attacks in this area were huge."

The insurgents are able to launch surprise raids, seize urban ground and hold their positions for days, weeks or even months, even far beyond their strongholds in the west. The growing disorder and violence threaten to open the country to interference by its neighbors and dash what little hope remains that ordinary Iraqis might benefit from their oil wealth.

Prime Minister Nouri al-Maliki has said he and the Iraqi army have the upper hand in the fight against terrorism. His spokesman said in an email that militant groups are "surrounded in certain areas, but the process of ending such battles does not happen easily."

Wednesday's vote is expected to extend Mr. Maliki's divisive eight-year tenure, which has alienated some of Iraq's already disparate ethnic and sectarian groups.

The Islamic State of Iraq and al-Sham, or ISIS, a militant group that grew out of al Qaeda but has broken with it, has declared voting stations and voters as targets, particularly in Baghdad. ISIS, which is expanding, has already staked out positions on the capital's outskirts.

"I see them gunning for Baghdad," says Jessica Lewis, research director for the Washington-based Institute for the Study of War and a former U.S. military intelligence officer.

Mr. Maliki, a Shiite, has proved unable to resolve the gridlock among the country's three main political blocs: Arab Sunnis, Shiites and Kurds. With his army unprepared to handle the fallout, foreign diplomats, politicians and analysts say Mr. Maliki is governing over a state that is failing in slow motion.

"Partnership failed in Baghdad," says Fouad Hussein, chief of staff to Kurdistan's regional president, Massoud Barzani. "After the election, if we cannot work together as three groups—Sunnis, Shias and Kurds—then Iraq is headed toward collapse."

Parliament hasn't met for nearly a month because of walkouts over a budget dispute between Mr. Maliki's allies and the Kurdish north. Iraq's Sunni minority, meanwhile, is accusing Mr. Maliki's military of ethnic cleansing under the guise of the fight against terrorism—a claim that has fueled Sunni calls for an autonomous region.

As stresses build, Mr. Maliki appears to be expanding his own writ as part of his push for a third term. Last month, he threatened to use a pliant judiciary to declare the gridlocked parliament constitutionally illegitimate. That would grant him sole authority to control the country's nearly $150 billion budget by presidential decree.

The latest violence began in late December when Mr. Maliki ordered security forces to disperse an anti-Maliki protest camp in Ramadi that he claimed was an incubator for al Qaeda.

The raid was akin to batting a hornet's nest. Thousands of well-armed Islamist militants rose up in early January in the surrounding province of Anbar and seized Ramadi, the provincial capital, and Fallujah, a restive city less than an hour from Baghdad.

ISIS's massive, sophisticated weapons arsenal suggested that the group had been importing weapons from Syria, says Gen. Dulaimi.

The militants displayed the kind of battle acumen lacking in Iraq's troops. Many ISIS fighters have returned battle-hardened from the conflict in neighboring Syria.

"The security forces were surprised that the militants were better equipped than the security forces themselves," says Gen. Dulaimi. "Our soldiers don't have anything more than AK-47s."

Iraq is still reeling from the U.S.-led Coalition Provisional Authority's decision in May 2003 to disband ousted President Saddam Hussein's army. The military had long acted as an adhesive bonding together young Sunnis, Shiites and Kurds. The move created a bitter underclass of well-trained young Iraqi men. Now, the leadership of the militias is populated by veteran generals from that disbanded army.

Despite nearly a decade of training from U.S. troops, the Iraqi army remains, by comparison, poorly equipped and far less motivated, say Iraqi politicians, Gen. Dulaimi and Hisham Hashemi, an Iraqi researcher on armed groups who is in regular touch with militants in Anbar.

Even the most basic maneuvers can stymie the Iraqi military. Regional commanders who lack basic knowledge of military logistics often are clumsy when transporting food for soldiers on the move, leaving many enlistees to scrounge for themselves or go hungry, say officers and observers.

Without meals, some soldiers simply leave. Though there are no official statistics, military personnel cite desertion as a persistent and growing problem, particularly for troops deployed in Anbar and other areas to the north where ISIS is active.

"There is hunger and a shortage of food," says Gen. Dulaimi, who is from Anbar province and was deployed there as an adviser earlier this year. "This is because commanders weren't trained in how to move troops from one area to another. Every day, we have troops who just don't show up."

The lack of readiness of Iraqi forces almost cost the general his life. In January, Gen. Dulaimi says, he was passing through a dense urban area of Ramadi in a column of nearly 50 Humvees, tanks and armored cars. They were ambushed by what he describes as hundreds of militants carrying machine guns, grenade launchers and improvised explosives.

When Gen. Dulaimi called a command center in Anbar for help, he was told that there were no airplanes capable of operating at night, he says. He was on his own.

"In the 1980s, during the war with Iran, we had to operate airplanes that could fly at night," he says. "Now, in 2014, we don't have that."

After nearly five hours, Baghdad sent a Russian-made prop plane loaded with two missiles—its maximum capacity. One of the missiles landed a direct hit, scattering the antigovernment commandos.

Still under sniper fire, Gen. Dulaimi got out of his Humvee and fled on foot.

The Ramadi firefight is just one of the frustrations and humiliations that Gen. Dulaimi says have allowed ISIS to expand north and east of Anbar province.

Iraq's military doesn't have adequate ammunition supplies, so it has had to use bullets and tank shells in combat that were supposed to be set aside for training. Because of a lack of armed-transport vehicles, convoys carrying heavy weapons and vehicles routinely come under attack on the road from Baghdad even before they have reached the battlefields of Anbar.

Gen. Dulaimi blames Iraq's losses on the U.S. Had Washington delivered Apache helicopters Baghdad has been requesting for several years, the army could have quickly ended the skirmish in which he was caught up, he says. Iraq's few armed helicopters aren't even outfitted with directed missiles—an anachronism in a modern fighting force, he says.

Requests for ammunition and sophisticated air power have gone unanswered, he says. Thirty-six F-16 jet fighters ordered in 2011 and last year—Iraq has no jet fighters in its tiny air fleet—have yet to be delivered, in part because of congressional objections to supporting the Maliki regime.

U.S. officials say they have worked to speed the transfer arms and already have provided some ammunition, small arms, Hellfire missiles and helicopters. The officials say they are ready to move forward with the F-16 sale and an Apache helicopter but are awaiting Iraqi payments and upgrades to military installations. They also have been trying to improve training of Iraqi forces.

American soldiers who helped train the Iraqi military say that Iraqis abandoned the organizational and educational infrastructure U.S. forces had hoped would perpetuate a professional military.

"The whole concept of developing a professionalized security force just stopped right there with the [end of the] U.S. presence," says Lieutenant General Robert Caslen, who was the chief of the U.S. military's Office of Security Cooperation in Iraq, which is in charge of training troops, from September 2011 until May 2013.

Gen. Caslen and his predecessors helped build and run an Iraqi military academy to feed trained personnel into Iraq's officer corps. On a visit about a year after U.S. troops left in December 2011, Gen. Caslen says, the academy was all but vacant.

To address the Iraqi military's logistical challenges, Gen. Caslen says, the U.S. built a large warehouse for spare military parts, complete with a computer automated inventory system.

"I went to that warehouse about a year later and all the parts were still beautifully on their shelves," he says. "But when you moved the parts, you could see they were covered in dust."

The computer system had been switched off because of frequent power cuts. Gen. Caslen says he was told that there wasn't enough gasoline available to run electrical generators.

Gen. Caslen says the Iraqis preserved ethnic and sectarian diversity in the military's upper ranks, as instructed by the Americans. But the nation's divisions permeated even that arrangement. Officers routinely bypassed the chain of command to deal with soldiers from similar backgrounds, the general says.

"There is a lot of distrust within the organization," says Gen. Caslen, now superintendent of the U.S. Military Academy at West Point.

Gen. Caslen and Gen. Dulaimi agree on one thing: The failures of Iraq's military are a function of the political disorder in Baghdad. Both men express little hope that Wednesday's election will usher in a government capable of reforming the military.

In the year since Gen. Caslen left Iraq, militants have taken aim at the palm forests and grasslands of northern Iraq that fall under Gen. Dulaimi's command. With a mix of Kurds, Sunni Arabs, Shiites and other minorities, the region is a crucible of the wider conflict fracturing the country.

In a series of interviews recently, villagers displaced from the hamlet of New Sari Tepe said they hadn't seen violence for nearly six years until the early morning of March 21, when a car bomb destroyed a nearby bridge. Residents stumbled out of bed to find masked fighters fanning out across the village and commandeering several houses near a local police barracks.

What followed was a six-hour firefight between the soldiers and militants. With the bridge into the town destroyed, reinforcements from the nearby town of Qara Tepe fired mortars from the far riverbank until the insurgents eventually scattered.

Instead of chasing the gunmen, the soldiers turned on the residents of the Sunni-majority village, complained Mr. Latif, the farmer. Troops raided the homes the militants had used for cover and arrested a dozen people, including two elderly men. All of them remain in prison without charge, villagers said. Security forces denied having any information about the detainees, said Ammar Mozahim, New Sari Tepe's provincial council representative.

Mr. Mozahim said a staff colonel who commanded a local battalion told him that because of the continuing fight in Anbar, there weren't enough troops to defend the region's small villages. Mr. Mozahim said he was told the military would be withdrawing much of its presence, leaving the countryside around Qara Tepe all but defenseless.

Gen. Dulaimi confirmed the decision, describing it as normal protocol considering the personnel limitations. Mr. Maliki's spokesman said "the army sometimes does tactical progress and retreat."

About a half-dozen similar attacks occurred in villages and towns along the Hamrin Mountains, a low-lying ridge pocked with caves that offer sanctuary to Islamist militants.

In Bohruz, a suburb of Diyala's provincial capital Baqouba, a squad of ISIS fighters killed an officer at a local police outpost. ISIS announced on militant Islamist websites that they would make Bohruz, a Sunni enclave in Shia-majority Diyala, their base of operations in the province.

The Iraqi military moved to clear Bohruz of ISIS insurgents by recruiting the local chapter of a Shiite militia called Asaib Ahl Al Haq, according to Iraqi media. By the following day, 28 villagers had been killed and several houses and mosques were torched, local officials said. Residents of New Sari Tepe said the news left them frightened of both Sunni insurgents and the Shiite-dominated military. So the mostly Sunni residents fled to nearby Qara Tepe.

New Sari Tepe remains abandoned and its bridges broken. Fed up with the Iraqi military, some displaced villagers said they hope Kurdish troops, who are widely considered more professional, will assume responsibility for the region.

"We're the victims of the both the government and the insurgency," said Mr. Latif. "We were afraid that if we were left behind, we would become the targets of both the security forces and the gunmen."