Afghan National Army soldiers stand guard in Laghman province on Aug. 9. An Afghan soldier turned his weapon on NATO allies on Aug. 9, killing three Marines. On Aug. 10, an Afghan police officer opened fire on Marines exercising on a base in Garmsir district, killing three. (WASEEM NAKZAD/AFP/GETTY IMAGES)

Until Friday night, not a single bullet had been fired at Kilo Company, 3rd Battalion, 8th Marine Regiment. The troops’ three-month tour in southern Helmand province had been calm, undisturbed by a diminished insurgency that operated beyond the perimeter of their base.

That lull was shattered Friday when an Afghan police officer opened fire on Marines exercising on a base in Garmsir district. Three U.S. troops were pronounced dead before midnight, U.S. and Afghan officials said. The assailant — a personal assistant of the district police chief — was detained.

The previous night, three other Marines were fatally shot by a rogue Afghan police officer who targeted them during a visit to a local checkpoint, officials said. The two incidents marked the 25th and 26th “green-on-blue attacks” this year. A total of 37 coalition service members have been killed by their Afghan counterparts since January.

About 24 hours after the second incident, the three bodies were taken to the airfield at Camp Leatherneck, Helmand’s largest U.S. base. The plane that would take the flag-draped caskets out of Afghanistan arrived after sunset. Fluorescent lights illuminated the flight line as several hundred NATO troops marched toward the C130, two by two.

Two armored ambulances pulled up next to the plane. There was one casket in the first and two in the second. The ceremony was held mostly in silence, punctuated by commands to salute the fallen, and the sound of the caskets sliding from the bed of the ambulance into the arms of the troops who would carry them down the runway.

A chaplain said the names of the three men — the first time many here learned which of their comrades had died.

“Let us pray,” he said.

One at a time, service members carried the caskets into the dark belly of the plane, bending at the knee and then offering a final salute.

“Those who served with them mourn their tragic deaths,” the chaplain said.

Top U.S. and Afghan generals then walked onto the plane and hovered over the caskets. Brig. Gen. Gulam Farooq Parwani, the deputy commander of the Afghan Army’s 215th corps, stood next to his American counterparts. He looked down at the three American flags.

“I thought to myself, ‘These men came from so far away to help our nation,’ ” Parwani said. “These are the worst incidents. It’s like losing family members.”

When the generals stepped away from the plane, Maj. Gen. Charles M. Gurganus, the top U.S. commander in Helmand, left the formation, walking behind one of the ambulances and disappearing for a brief moment alone. He looked away from the ceremony and into the distance, where there were blast barriers and fences and, beyond that, miles of desert.

When Marines first arrived in Helmand in 2009, waging one of the war’s bloodiest battles, such “ramp ceremonies” were far more common than they are today. Now, the battle in Helmand has entered a new chapter — a swift drawdown and a concerted push to ready Afghan security forces. A year ago, Marines were in more than 60 locations throughout Garmsir. Now, Marines are based in three sites in Garmsir.

Progress is visible. Marines can walk through local bazaars and across the district center in Garmsir. They can take off their body armor when they meet with large groups of local elders. At Forward Operating Base Delhi, where Friday’s attack took place, there was time for diversion — even an Olympics-style competition where Marines faced off against one another in weightlifting and running events.

When they were attacked, the Marines were exercising in a gym on a part of their base used as the headquarters of the Afghan district police. That American troops felt comfortable putting their weapons down and exercising with their Afghan counterparts marked the strength of the partnership, officials said. U.S. troops at Delhi sometimes complained about the Afghan police work ethic, but the relationship was healthy. There was no reason to worry about internecine violence.

The Afghan officer who targeted the Marines worked as a low-level assistant — delivering tea and doing basic administrative duties. U.S. officials would not discuss the incident, citing an ongoing investigation. But the Taliban promptly took credit for the attack.

On Saturday, Afghan officials said another Afghan police officer killed at least 10 of his colleagues in Nimruz province in southwestern Afghanistan after he, too, was recruited by the insurgency.

At Delhi, the investigation kept the Marine leadership up all night. Top Afghan security officials were investigating, as well, working to discern the assailant’s motives.

“He was high on drugs and claims he lost control,” one Afghan official familiar with the investigation said.

“It was another effort by the enemy to come between us and the coalition,” Parwani said.

The ramp ceremony lasted about 20 minutes Saturday night. When the Marines dispersed and the plane sat alone on the airfield, its pilots turned their engine on and waited for the sign that it was time to depart.