KABUL — The 29-year-old was a gung-ho Afghan army captain with a university degree, fighting a war with bullets and Facebook posts against an enemy with a medieval vision of Islam. Often, he posted photos of himself, hefting a weapon or praying in a field, between bouts of fighting in northern Afghanistan.

A few weeks before he died, Lemar Safi posted an image that posed a defiant question to the Taliban. In it, he is sitting on a low mud wall, with his rifle resting beside him, holding up a hand-lettered sign. “I am Muslim,” it reads in large English letters. Above, in Dari script, it adds: “Do you really want to kill me?”

That was in the spring of 2017, in Kunduz province, where Safi had spent more than a year on combat duty with the Afghan National Army and fought in a battles for control of the province’s strategic capital and main highway. Dozens of his comrades perished. Safi was shot by a Taliban sniper in a predawn ambush and died instantly, according to his commanding officer.

On Saturday, the Taliban launched a fierce assault on Kunduz city, attacking from several sides as residents fled intense fighting. At least 10 people were killed in an insurgent suicide bombing. A government spokesman said the attack, which came as U.S. and Taliban officials were finalizing a possible peace agreement in Qatar, showed that the insurgents “do not accept the opportunity for peace” being offered.

Nevertheless, Safi’s parents in Kabul have been praying that the peace process will bring a permanent end to decades of conflict, destruction and personal loss. Their first son, Khalid, died in Afghanistan’s civil war during the early 1990s. Another son, Hamun, 44, a high school principal, lost his left leg in a land mine explosion during military training in 2005.

Safi’s message to the Taliban was a pointed challenge to the insurgents’ assertions that they are a pure Islamist movement fighting an “infidel” foreign army and its Afghan minions. The vast majority of those killed by the Taliban have been fellow Afghan Muslims, including civilian government workers, bombing victims and security forces. 

“Lemar’s death has been a heavy burden for our family to bear, but we don’t believe that his sacrifice was for nothing,” said Khybar, 38, another brother who works in the Afghan judicial system. “Thousands of soldiers like him have died for the same cause. He wanted to fight to defend his country and its future as a democracy. He lost his life, but he did it for the sake of peace.”

The Afghan government does not release casualty numbers for security forces, but President Ashraf Ghani said in January that 45,000 soldiers and police had been killed since he took office in 2014. In recent months, Taliban and Afghan forces have been fighting aggressively to gain advantage in the peace negotiations. The high rates of casualties among civilians and armed forces, including those from U.S. airstrikes, have intensified public demand for peace.

Both sides in the talks in Qatar say they are close to reaching an agreement that would allow 5,000 U.S. troops to leave in return for the Taliban promising not to let al-Qaeda operate from areas it controls. It is not clear whether negotiators have resolved disagreements over a permanent cease-fire, a timetable for remaining troops to leave or whether the Taliban is willing to meet with Afghan officials. 

By all accounts, Safi was an enthusiastic soldier with an unusual profile. He had a college degree in child psychology and a talent for poetry. He was not married, and he had spent time as a translator for the U.S. military. To his family’s initial dismay, he decided to enlist in the army when he was 26. He enrolled in its officer academy and graduated as a captain one year later.

“We wanted him to stay home, to become a teacher, but he had strong feelings about the war, about wanting to stop the Taliban and the terrorists,” said his father, Mohammed Akram, a retired security officer in his 70s.

In 2016, Safi was assigned to a combat unit in Kunduz, a province near the border with Tajikistan where the Taliban had briefly seized the capital in weeks of heavy fighting the year before. The surrounding region and link roads remained under siege for months. Safi soon became known as a driven fighter who took risks and inspired others to do the same. 

“He hated to hang around the base and always wanted to be out looking for the enemy,” Lt. Col. Hamid Saifi, his former commanding officer, said in a phone interview from another province. “He volunteered for everything. His morale was very high. We rarely see such soldiers in the army.”

Saifi described intense skirmishes with the Taliban during that period. At one point, the army’s forces were trapped for five days under fire in the Khanabad area. At another, he said they were cut off by Taliban fighters and unable to reach the provincial capital. Safi’s unit, he said, “established a strong line and fought hard,” opening up the way.

“I lost a lot of men in that war,” the commanding officer said. “Then I lost him.”

Safi also had an affinity for social media with an exhibitionistic flair. He frequently posed for war-zone photos and posted them with comments. He spoke to his family by cellphone almost daily, reassuring them that everything was fine. He recorded and sent out dramatic videos, including one of a village patrol, punctuated by bursts of gunfire, that shows soldiers scrambling and shouting.

Akram said his son had dreamed of joining the army’s special operations forces but died while waiting for his application to be reviewed. Hours after the family got the news from Kunduz, an army official in Kabul called to say they needed his final paperwork for the transfer. 

“We told the man we were waiting for his body,” Akram said. 

There was no posthumous military award, but Safi’s family received a $2,000 payment that the Afghan Defense Ministry provides in all war deaths. Help for Afghan Heroes, a nonprofit group in Kabul that assists families of slain or disabled service members, arranged for his death certificate to be sent from the war zone so the funds could be processed.

Safi’s death received considerable attention among Afghans on social media, where his photos and poems from the front lines had attracted thousands of followers on Facebook. His relatives also organized activities to commemorate him, making posters and banners to display on holidays. 

In the lobby of the high school where Safi’s brother Hamun is the principal, near the family’s apartment in a crowded suburb of east Kabul, a large wall-hanging depicts several of Afghanistan’s historic heroes, including King Amanullah Khan, a reformist monarch from the 1920s, and the late anti-Taliban commander Ahmad Shah Massoud.

There are also four photos of Capt. Lemar Safi in uniform, including one with his pointed message to the Taliban: “I am Muslim.”