Four U.S. military personnel were detained for several hours by Libyan authorities on Friday while they were examining potential evacuation routes for diplomats, in an incident that underscored the danger that American officials continue to face in the North African country.

Shortly before midnight, the State Department confirmed the four men had been released. “We are still trying to ascertain the facts of the incident,” State Department spokeswoman Jen Psaki said. She added that the personnel were working on “security preparedness efforts when they were taken into custody.”

U.S. officials provided few details about the circumstances that led to the men’s detention or how their release was brokered.

The detention came to light after photos of the biographical page of two American men described as Libyan government detainees were posted on social media. Another photo, of blue U.S. Embassy badges with matching names, was circulated on Twitter. A third photo posted on social media showed the men sitting on a torn-up black sofa, looking dour.

As of late Friday, Libyan government officials did not appear to have provided an explanation of why the men were taken into custody.

A U.S. defense official said the men were posted at the embassy in Tripoli to augment its security force. “We think they were reviewing possible evacuation routes for embassy personnel when apprehended,” said the defense official, who spoke on the condition of anonymity to discuss a sensitive investigation. “There’s a lot we still don’t know at this point.”

Social media users who circulated the photos said the men were detained in Ajaylat, a small city in northwestern Libya near the Tunisian border.

Libya’s nascent government has struggled to consolidate authority in the wake of the 2011 ouster of dictator Moammar Gaddafi. Militias that emerged during the country’s civil war remain dominant in many parts of the country, acting as a de facto government authority in several regions. Arbitrary detentions in the country have become so commonplace that even the country’s prime minister was briefly taken hostage this year.

The tenuous security situation in postwar Libya became painfully clear to the United States the year after Gaddafi’s ouster, when a sophisticated attack on U.S. government compounds in the eastern city of Benghazi resulted in the death of four Americans, including Ambassador J. Christopher Stevens.

The Sept. 11, 2012, attack became a major political controversy for the Obama administration as Republicans on Capitol Hill questioned why the State Department hadn’t heeded calls for more security for diplomats in Benghazi and implied the White House had orchestrated a coverup.

In the wake of the attack, the U.S. military has repositioned troops in southern Europe and drawn up plans to respond more quickly and assertively to attacks on U.S. diplomats and personnel in North Africa.