Ammar, an Iraqi Shiite Muslim, was so eager as a teenager to join his country’s army that he considered lying about his age. Three years after he finally joined, he found himself defending an oil refinery as it came under attack in late June by Sunni militants.

What happened next left him convinced that the Iraqi army was broken: His brigade commanders fled, leaving their men behind.

And with that, Ammar and about 400 fellow soldiers also decided that night to leave the refinery, joining the thousands of Iraqi troops who have deserted since the Islamic State began capturing territory across northern Iraq last month.

Over the past three weeks, nearly one-tenth of Iraq’s 700,000 active soldiers have shed their uniforms, according to Michael Knights of the Washington Institute, who has extensive contacts in the Iraqi military. Iraqi officials have estimated that the number might be as high as 90,000.

The Iraqi government is now rushing tens of thousands of new recruits through basic training, and it has solidified alliances with Iran-backed Shiite militias and enlisted their help in joint operations. But experts and Iraqi soldiers say additional manpower is unlikely to remedy a weakness that contributed to the army’s collapse and is highlighted by Ammar’s account of the chaos that unfolded at the Baiji refinery: poor leadership.

How the Islamic State is carving out a new country

“Our officers sold us out,” said Ammar, who, fearing government reprisal, spoke on the condition that only his first name be used. “They abandoned us.”

‘I wanted to defend my country’

Since he fled, Ammar has evaded authorities, who are arresting deserters, by moving between the houses of friends and family in his home city of Baghdad. It was there that, as a bored teenager cooped up during the U.S. occupation, he tried to get a fake ID to dodge enlistment-age restrictions.

“I wanted to defend my country,” said Ammar, who, in his tight T-shirt and acid-washed jeans looks more like an Arab Idol contestant than a soldier. “Also, it was something to do.”

After joining in 2011, he was assigned to the 37th Brigade of the Iraqi army’s 9th Division, guarding the Baiji refinery, a facility north of Baghdad that once produced one-third of the country’s fuel. The size of a small town, the fortified complex includes dozens of buildings scattered around towering metal vats in which crude oil is processed into fuel, a snaking maze of pipelines and rows of cylindrical storage tanks, each one as big as a sports arena.

When the Islamic State captured the northern city of Mosul last month, Ammar’s commanders likely knew the fighters were coming to Baiji. During last decade’s Sunni insurgency, al-Qaeda in Iraq made millions from the refinery through an extortion and smuggling racket. Now, that organization’s successor surely understood that gaining control of the facility could give it a huge boost in fuel and income while dealing a severe blow to the government.

When the militants attacked on June 18, Ammar could tell they weren’t from the local tribes. These men had long beards and wore Afghan-style tunics, said Ammar, whose account of the attack could not be verified but was corroborated by several soldiers in his brigade, refinery workers, Iraqi oil officials and a leader of tribal militants in the area.

“After each battle, we dragged their bodies inside the refinery and checked them. They were DAIISH,” Ammar said, using an Arabic acronym for the Islamic State.

At first, the soldiers had several advantages, including the refinery walls and guard towers, from which they could shoot down at attackers. His brigade of more than 400 soldiers had also been reinforced by a team of about 50 special forces troops flown in from Baghdad.

At dawn on June 23, militants attacked from two directions, using a combination of gunfire and bulldozers. After knocking down fences, dozens of fighters streamed into the complex and took over three guard towers.

Once the militants were inside, both sides were constrained in the weaponry they could use and where they could shoot, because they were surrounded by pipelines and containers filled with highly flammable fuel.

“We were afraid the whole place would explode,” Ammar said.

It took a full day of fighting to drive out the militants. Fifteen soldiers were injured and five were killed, including three whose bodies had been wired with bombs.

A shocking reversal

Ammar and his comrades said they never had any illusions about the virtue of their officer corps. Iraqi soldiers could often avoid showing up for duty by giving half their salaries to their commanders, who in turn paid off the generals. Even before Mosul fell, this cycle of bribery and absenteeism had deprived the military of almost one-third of the 1 million troops receiving paychecks, according to Knights.

Still, Ammar said what happened after the June 23 battle shocked him. At sunset, a minibus was ushered through a refinery gate. His commanding officers — including a brigadier general, a colonel, and a lieutenant, he said — quickly began boarding.

“I said, ‘Where are you going?’ ” Ammar said. “And they told me, ‘We reached a deal with the tribes. You can leave safely now, but if you stay, you’ll be killed.’ ”

Tribal leaders in the area had recently banded together into an organization called the Military Council for Revolutionary Tribes. A leader of the Baiji tribal council, who calls himself Maj. Khalid al-Iraqi, confirmed that the group had acted as a mediator between the militants and the soldiers at the refinery.

As they were leaving, Ammar’s officers told him the military council had promised to provide safe passage out of Baiji for 24 hours.

“We only found out because we stopped their car,” Ammar said.

Once the minibus drove off, the soldiers began arguing among themselves about what to do. Meanwhile, the elite commandos — colloquially referred to as SWAT forces — tried to restore order.

“The commander of the SWAT, a captain, he was talking nonsense,” said one of Ammar’s friends, who gave his name as Mohammed. “He said, ‘Don’t give up the refinery! Fight against DAIISH!’ ”

Ammar said he concluded that if he didn’t run he would probably die. They were low on ammunition; the refinery complex had lost electricity and running water; and helicopters providing air support and supplies had stopped coming because the militants had antiaircraft guns mounted on pickup trucks.

Ammar joined a group of about 100 soldiers, who told the others they would test the deal offered by the tribes. If they weren’t killed, they would call to say it was safe to follow. The elite commandos were furious.

“They threatened to execute any soldier who tried to leave,” Mohammed said.

The commandos drew their weapons. The Iraqi army soldiers drew theirs. The elite forces were badly outnumbered.

“We said to them, ‘If you shoot at us, we’ll shoot at you,’ ” Ammar said. “So they backed down.”

Ammar changed out of his uniform and left on a minibus in the middle of the night.

‘They let us down’

Despite the disappearance of the 37th Brigade, Baiji ultimately did not fall.

The elite commandos retreated to a corner of the complex and held out for days. When reinforcements finally arrived, the commandos battled outward to chase off the militants, who still control the surrounding area.

Ammar’s war was over. The afternoon after he left Baiji, he was lounging on a dusty berm by a checkpoint, waiting to enter Irbil, the capital of Iraq’s semiautonomous Kurdistan region. Defended by its own highly motivated army entirely separate from the Baghdad chain of command, Kurdistan has received hundreds of thousands of refugees in recent weeks, including thousands of soldiers.

Even if it were safe to return to the army, Ammar said, he probably wouldn’t.

“I don’t think I’ll go back,” he said. “They let us down.”