The U.S. Consulate in Benghazi is seen in flames on Sept. 11, 2012. (ESAM OMRAN AL-FETORI/Reuters)

On a rooftop at a CIA base in Benghazi, Libya, as he bled from his skull, gaping arm and a nearly severed leg, State Department diplomatic security agent David Ubben noted that three lethal mortar rounds had fallen closely clustered.

The mortar attack over 45 seconds in September 2012 killed two of his comrades and wounded him.

“They all seemed to land within meters of each other, in an area the size of maybe that conference table,” Ubben, 36, testified, pointing Wednesday to prosecutors’ eight-seat table at the Washington trial of accused attack ringleader Ahmed Abu Khattala.

The tight pattern, he testified, and the timing of the strikes moments after a CIA security contractor team arrived from the airport and took up position on an north-facing wall, suggested a precision attack by well-trained adversaries using targeting equipment.

Ubben’s dramatic yet clinically calm testimony on the third day of the murder and terrorism conspiracy trial of Abu Khattala, 46, helped lay a cornerstone of the U.S. government case against the Libyan militia leader. He has pleaded not guilty to 18 charges accusing him of leading an attack Sept. 11, 2012 on a U.S. diplomatic compound and, after midnight struck, of an assault Sept. 12 on a CIA annex.

In an indictment after Abu Khattala’s June 2014 capture in Libya by American commandos and in opening statements Monday, Justice Department prosecutors told jurors that Abu Khattala thought the diplomatic mission was cover for an illegal U.S. intelligence facility in Benghazi, allegedly asking at a meeting in a mosque a year before the attacks, “How can we allow spying among us?”

Ubben appeared to serve at least two purpose for prosecutors.

Under questioning by Assistant U.S. Attorney Julieanne Himelstein, he proposed a plausible and crucial link between Khattala and the two attacks separated by several hours. He also gave the Washington jury a compelling and sympathetic portrait of human suffering and bravery.

“It is gruesome to look at,” Ubben said offhandedly at one point, lifting a pant cuff to show jurors his leg injury, and raising his hair to show a nearly ear-to-ear scar from skull surgery to repair shrapnel damage that tore across his head.

Abu Khattala led a brigade absorbed by the Ansar-Al Sharia militia, a U.S.-designated terrorist group whose immediate public claim of responsibility for the mission attack was confirmed by U.S. investigators.

Prosecutors allege it was only after Abu Khattala traveled after midnight to a militia headquarters with maps, charts, computers and other sensitive information looted from the diplomatic site that the secret CIA annex about a mile away came under small-arms fire, at about 12:45 a.m.

On Monday, defense lawyers said Abu Khattala came to the mission out of curiosity, was directing traffic to keep bystanders safe and entered only after the fighting was over and Americans had left.

Defense attorney Jeffrey D. Robinson reminded jurors that although prosecutors said Abu Khattala masterminded the conspiracy, they acknowledged he was home by the time of the lethal mortar strikes on the annex at 5:45 a.m.

“You don’t go home if you are planning and leading an attack on a C.I.A. annex,” Robinson said.

In a court filing to U.S. District Judge Christopher R. “Casey” Cooper, who is presiding over the trial, prosecutors said they plan to introduce evidence that Abu Khattala had access to and was skilled with mortars, and trained at least one of his men who was caught in surveillance video participating in the mission attack in firing mortars.

Ubben testified that the map over his desk in a mission office and others like it contained grid coordinates for the CIA post — “55J,” he said — a map “the same size and color” of rolled-up or folded maps that surveillance video showed a looter carrying out of the office in the attack.

But Ubben, under questioning by Robinson, replied that he did not have information about Abu Khattala’s activities that night.

“You don’t know anything about what Mr. Khattala did?” the attorney said at one point.

“That’s correct,” Ubben said.

U.S. Ambassador J. Christopher Stevens and State Department communications aide Sean Patrick Smith died of smoke inhalation in the fiery diplomatic mission, and CIA contractors Tyrone S. Woods and Glen A. Doherty died in the mortar attack on the CIA annex.

Speaking in a level, dispassionate tone broken only rarely by deadpan humor, Ubben described recovering the body of Smith from the burning villa andriding in an armored vehicle through a partial roadblock of Libyans on the streets while evacuating to the CIA annex, where he at one point volunteered to relieve the CIA security contractors on the roof.

“Well, the first mortar exploded . . . I was hit by shrapnel on my left side into my temple,” obscuring his vision with blood, said Ubben, a U.S. Army veteran of Iraq. “I’ve been under mortar fire fairly frequently in my life . . . I heard that whistling sound so I knew we were going to get more.”

The next mortar hit to his left, silencing the returning fire of Woods and Doherty, who were killed. A third round struck on his right “very close. I could feel the heat from the blast. It was intensely painful. A lot of light,” Ubben said.

Ubben’'s lower right leg was mostly severed. He tried to apply a tourniquet from his medical kit, but said when he could not perform the “two-handed” operation, “I went and looked at my left arm, and I had a grapefruit-sized chunk taken out above the elbow . . . I thought, ‘That’s great,’ and noted I’m going to need a second tourniquet.”

With casualties mounting, the Americans evacuated to the Benghazi airport. A powerful colleague carried the 6-foot-4-inch, 220-pound Ubben down a ladder, Ubben testified. He rode to the airport lying in a litter in the back of small SUV whose rear hatch could not be shut because of Ubben’s height.

“The whole route I was sliding out the back. I had to shout at the driver to stomp on the brakes” so momentum would move his body toward the front of the small SUV. “With my good leg,” Ubben said, he braced himself. “So there’s a little bit of comedy there.”

Ubben said his condition was stabilized after two weeks at the U.S. military hospital in Landstuhl, Germany, and he spent 15 months undergoing 40 surgeries, including major operations as frequently as every 48 hours.

“Through the grace of God and because of the excellence of the doctors, nurses and therapists at Walter Reed [National Military Medical Center], and lot of hard work, I was able to save my leg more or less and relearn how to walk and how to run,” Ubben said. “There’s no normal. I’m now a new person . . . But yes, at this point I’m able to perform and do my duties at a high level.”