U.S. Navy SEALs had walked nearly seven miles from their landing zone in southern Yemen and were within about 300 feet of the al-Qaeda compound where American journalist Luke Somers was being held when they suddenly came under fire, U.S. officials said Saturday.

As the commandos battled in the darkness, night-vision cameras in aircraft hovering overhead watched one militant hurry to the building housing Somers. By the time the Americans fought their way there, the militant was gone and Somers and another hostage, South African teacher Pierre Korkie, lay mortally wounded.

Korkie died aboard a rescue aircraft, according to officials who provided details of the operation. Somers survived to reach a nearby Navy ship, the USS Makin Island, where he died while undergoing surgery.

No casualties were reported among the 40-person U.S. rescue team.

In the wake of the rescue attempt, which took place at 1 a.m. Saturday, Yemen time (5 p.m. Friday in Washington), officials said that the decision to undertake it was made after U.S. intelligence determined his al-Qaeda captors were about to execute Somers.

Luke Somers and Pierre Korkie (European Pressphoto Agency)

But the failure to bring the hostages out alive, following two earlier attempts to rescue U.S. captives in recent months, was likely to raise questions about the operations and the intelligence that preceded them.

Last summer’s raid to rescue Americans being held in Syria by the ­Islamic State, and an attempt to rescue Somers two weeks ago, did not succeed because in both cases the hostages had been moved before the commandos arrived. Two of the Islamic State hostages, journalists James Foley and Steven Sotloff, were later executed.

In a video released Wednesday following the initial attempt to rescue Somers, al-Qaeda in the Arabian Peninsula (AQAP) warned “Obama and the American government of the consequences of proceeding ahead in any other foolish action.”

The threat to execute Somers within 72 hours, if unspecified demands were not met, led to operational planning for a second rescue attempt, which President Obama approved early Friday. Officials said U.S. intelligence gleaned from the first attempt contributed to their certainty of where Somers was being held, in a group of compounds that make up a small village in Shabwah governate, a remote region along the Gulf of Aden. Defense, intelligence and administration officials spoke on the condition of anonymity to provide details of the secret operation.

The commandos did not know of Korkie’s presence in the compound. They had unconfirmed reports that another hostage might be with Somers but thought it would be someone else, a British citizen also being held by AQAP.

The tragedy of the South African’s death appeared to worsen when the South African relief organization that employed him, Gift of the Givers Foundation, said that his negotiated release had been expected Sunday.

Korkie, who had been working as a teacher in Yemen, and his wife, Yolande, were abducted in May 2013. Yolande Korkie was released from captivity in January, according to a post on the foundation’s Web site.

Somers, 33, a British-born U.S. citizen, was abducted in September 2013 from the Yemeni capital, Sanaa, on a busy street near a supermarket. He had been working in the country as a freelance photojournalist.

Hostage executions, video­-recorded by militants and disseminated online and via social media, have placed increasing pressure on the Obama administration to launch rescue attempts. While a number of militant-held European hostages have been released in exchange for million-dollar ransoms, the administration has said that payment will only increase the number of hostages taken. It has refused to pay and has pressured other governments not to participate in ransom negotiations.

Although the administration last spring traded five Taliban detainees from the prison in Guantanamo Bay, Cuba, for a U.S. serviceman captured in Afghanistan and being held in Pakistan, it described that negotiation as an exchange of war prisoners rather than a hostage ransom.

The three failed attempts to rescue hostages alive through military means come as U.S. Special Forces have conducted a series of successful raids to capture wanted militants. In June, Ahmed Abu Khattala, indicted for alleged participation in the 2012 attack in Benghazi, Libya, that left four American officials dead, was snatched in a raid in Libya. He is awaiting trial in this country.

But taking a suspect off the street at a time of U.S. choosing and rescuing hostages held by well-armed militants anticipating a raid are different matters.

As described by U.S. officials, about 40 Navy SEALs were transported from the USS Makin Island, said to be located “in close proximity to Yemen,” aboard V-22 Osprey aircraft that fly like airplanes but can land like helicopters. Using night-vision equipment, they began walking over terrain described as “hilly, scrubby” and “rough.”

As the team members approached the compound and were about 300 feet away, “they lost the element of surprise,” an official said, and a firefight with the militants began. Officials said they were not certain how the team had been spotted.

Because of the direction of fire, a defense official said, they were nearly 100 percent certain that Somers and the other hostage were killed by the AQAP militants and not in crossfire.

Via overhead surveillance that was maintained throughout the rescue attempt, one militant was seen going into the building where Somers was known to be held, a senior administration official said. The militant stayed “for about a five to seven count . . . long enough, of course, to shoot people or take any other action. We didn’t have visibility inside.”

By the time the commandos reached the building, the administration official said, “the terrorist had already fled.”

The defense official said five AQAP militants were killed in the firefight. None were captured.

The wounded hostages, found inside the building, were immediately evacuated aboard an Osprey, but “one perished on the way” to the USS Makin Island and “the other on the operating table” aboard the ship, an amphibious assault vessel.

The U.S. forces were not on the ground more than 30 minutes, said the defense official, who described it as “a quick fight.”

A flurry of Twitter postings from jihadists in Yemen claimed that three U.S. “Marines” and eight Yemeni special forces were killed during the operation.

Reached Saturday by the Associated Press, Somers’s sister, Lucy Somers, said the family had been notified of his death by the FBI but asked that the family be “allowed to mourn in peace.”

Since Somers’s kidnapping last year, his family had appealed for news of the abduction not to be made public. But after the AQAP video was released Wednesday, the family released their own video.

“He is a good person, and he has only been trying to do good things for the Yemeni population. He goes out of his way to care for and respect the common person, and he has made many lasting friends in Yemen,” Somers’s brother said in the video. “Luke is only a photojournalist, and he is not responsible for any action the U.S. government has taken. Please understand that we had no prior knowledge of the rescue attempt for Luke and we mean no harm to anyone.”

His mother also spoke, pleading for mercy.

“Give us an opportunity to see our Luke again. He is all that we have.”

Missy Ryan in Kabul contributed to this report.