After an immigration roundup in Virginia this spring, federal officials announced that they had captured a former lieutenant in a Somali national-security agency notorious for human-rights abuses.

But retired federal agents tell a different story about the thin, bespectacled father who has been sitting in an immigration jail since late March. They say he is a confidential FBI informant who risked his own safety to save American lives and should have received a Green Card. Instead, he faces deportation to a country where his history of spying for the United States could get him killed.

The man, whose name is being withheld by The Washington Post out of concern for his safety, prevented a “major terrorist attack” on a U.S. embassy in Africa, uncovered support networks for some of the 9/11 hijackers and probed the killings of U.S. servicemen in Somalia, according to confidential affidavits from retired federal agents shared with The Post.

“He’s getting a raw deal,” said Richard J. Lauria, a former immigration special agent assigned to the country’s Joint Terrorism Task Force. “He went above and beyond, and he shouldn’t be in the position that he is now.”

The man’s case illustrates the powerful leverage that the U.S. government wields over foreign informants and witnesses and the trade-offs for both sides. The government has long offered special visas to immigrants who provide critical information about terrorism or criminal organizations, even if those immigrants are here illegally or have criminal records themselves.

The visas lead to a Green Card or U.S. citizenship. But critics say the United States often does not keep its side of the bargain, leaving people such as the Somali man in legal limbo as long as they cooperate — and at risk of deportation when they stop.

The man became an informant to avoid deportation after being arrested for criminal immigration fraud in 1998. He continued that work off and on for nearly two decades, even as he accumulated minor criminal charges. He is now middle-aged with U.S.-born children. He said he told his latest FBI handlers early this year that he would not work for them anymore. Shortly afterward, he was arrested.

U.S. officials say immigrants are not entitled to an S visa — known derisively as the “snitch” visa — in exchange for their work. And they say informants who commit new crimes may disqualify themselves from the program.

“There are strict statutory requirements,” Department of Justice spokesman Peter Carr said last week. “Neither a law enforcement agency nor a federal prosecutor has the authority to guarantee an alien that they will receive an S visa.”

Because immigration files are kept secret, the full record of the Somali man’s case was not available for The Post to review. The FBI and Immigration and Customs Enforcement declined to give details about his work for the government or why he was arrested after 20 years in the United States. ICE spokeswoman Carissa Cutrell said the man has convictions for felony immigration fraud and multiple misdemeanors, adding that she could not provide additional information because the man refused to sign a waiver.

Congress created the S visa program in 1994, a year after the bombing of the World Trade Center in New York. The government sets aside 200 visas a year for informants and witnesses who provide information about criminal organizations and 50 a year for immigrants who assist terrorism investigations.

A law enforcement agency must sponsor immigrants for S visas, with the approval of the Department of Justice, the State Department and the Department of Homeland Security. Since 1995, nearly 900 immigrants, and 750 of their relatives, have received S visas for exposing criminal enterprises, according to federal statistics and the Congressional Research Service .

But only six visas have been given to terror informants or witnesses since the program began. Five visas were issued in 1995 and one was given out last fiscal year, in addition to six visas for relatives of those individuals.

In Georgia, attorney Tracie Klinke said she has had at least two cases in which clients were promised visas for aiding criminal investigations, including drug trafficking, but never received them. A former client from India who busted an immigration-fraud ring ended up terrified that he would be deported to the same country as the people he helped put in jail.

Hassan Shibly, executive director of the Council on American-Islamic Relations in Florida, said the FBI routinely recruits Muslim immigrants as informants but rarely follows through with promises to help their immigration cases.

“Immigrants, especially those who are not U.S. citizens, are prime targets because they [U.S. officials] have more leverage,” he said.

The Somali man’s path from immigration violator to government informant living in Northern Virginia began on a wintry day in 1995, when he landed at Boston’s Logan International Airport using a fake Swedish passport he bought for $500, court records show.

He applied for asylum, lost and was expelled from the United States. He returned to the United States, married and applied for asylum again, using his real name. Immigration agents checked his fingerprints, discovered the old case and arrested him for felony immigration fraud in 1998.

Lauria, the now-retired federal agent, said he recruited the man to investigate Somali war criminals who were being smuggled from Kenyan refu­gee camps to the United States and Canada. Court records say the man faced up to 10 years in prison and a $250,000 fine, not to mention deportation, but after he pleaded guilty, a judge sentenced him to one year’s probation.

For the next two decades, on and off, the man helped the FBI and other agencies investigate extremists and terror plots.

He wore hidden microphones to coffee shops, shopping malls, taxi stands and clubs, Lauria said. He told no one, not even his wife, what he was doing. She feared he was having an affair because he would stay out all night.

Tim Clemente, a retired FBI special agent, said in an affidavit that the man exposed terror groups’ financial networks in the United States and elsewhere. The investigations led to arrests and saved lives, the affidavit said, particularly in the case of the planned attack on an unnamed U.S. embassy in Africa.

Clemente wrote that it is “very likely that many, many people would have been killed” if the man had not helped.

Lauria said that before he transferred to another federal agency in 2001, he submitted a thick packet recommending the man for an S visa. But Lauria and others moved on, and now Lauria says he fears that the visa request fell through the cracks.

In 2012, Lauria and Clemente wrote the affidavits urging an immigration judge to grant the man legal residency.

“In my eyes,” Clemente wrote, the man “truly is an American hero.”

In an interview, Lauria said the man’s “days would be numbered” if he were deported.

The man says he told the FBI two weeks before his arrest that he would no longer be an informant. On March 26, ICE arrested him and said he had been a “second lieutenant in command of the Somali National Security Service, an organization known for human rights abuses, rape, torture and extrajudicial killings.”

The man denies that he was ever in the Somali National Security Service.

His U.S. criminal record includes the 1998 immigration fraud felony and, according to ICE, multiple misdemeanors. Court records show misdemeanor convictions in 2015 for prescription-drug possession and at least two pending charges: an arrest last year for obtaining money under false pretenses and a January charge for shoplifting.

Justice Department officials said there are many reasons informants and witnesses might not receive an S visa, including failure to maintain a clean record.

But Lauria and others said the man should have been a U.S. citizen by now, which would protect him from deportation.

On April 21, the man appeared via video in immigration court at a bond hearing. Clemente and a third retired federal agent, who gave his name only as Brian, were in the room, ready to testify on his behalf.

The judge considered closing the proceedings to the public. The Post objected, but the judge ultimately ruled that the man was not eligible for a bond hearing because of his criminal record.

His next appearance is in June.

“They are using this as a leverage for me to continue to cooperate,” the man said in an interview before the April hearing. “Or otherwise, ‘we’re gonna send you back to Somalia,’ where I will definitely be killed.”

Jennifer Jenkins contributed to this report.