(CNN) -- An immigration judge on Friday rejected the federal government's attempt to deport an Egyptian immigrant who had been acquitted of charges of illegally possessing and transporting explosives.
Youssef Megahed was released after being detained as a suspected terrorist for almost five months by U.S. Immigration and Customs Enforcement, or ICE, in a politically explosive case that has pitted national security claims against charges of profiling and discrimination against Muslims.
A spokeswoman for the Justice Department's Executive Office for Immigration Review, Elaine Kornis, indicated Friday that the government would appeal. However, ICE spokesman Richard Rocha later said the agency is reserving its right to appeal pending a review of the judge's written opinion.
Megahed was released on his own recognizance, according to a court administrator. He needs to report once a month to a local ICE office and "not associate with known terrorists," said Charles Kuck, the attorney for Megahed.
Megahed, a student at the University of South Florida, came to the United States from Egypt in 1998 as a legal immigrant, when he was 12. His problems started two years ago when, as a university engineering student, he went on a road trip with a new friend, Ahmed Mohamed.
The men were pulled over for speeding near Charleston, South Carolina.
Police said a search of their vehicle turned up PVC pipe with potassium nitrate inside, along with a detonator cord that was inside one of Mohamed's bags.
The government said the materials were "low explosives." Mohamed said they were materials for homemade model rockets.
Megahed said he did not know that the materials were in the car.
A search of Mohamed's laptop computer found research concerning rockets and propellants and how to manufacture them, as well as information about Qassam rockets -- crude rockets used by terrorists in the Middle East, according to prosecutors.
Prosecutors also had a video, made and narrated by Mohamed, in which he demonstrates how to outfit a model car with explosives. He posted it on YouTube.
Mohamed pleaded guilty to providing material support to terrorists and is serving a 15-year prison sentence.
Megahed, however, was found not guilty of two charges of possession and transportation of explosives and was set free.
Three days after his acquittal, however, he was detained again on grounds that he was "engaged or likely to become engaged in ... terrorist activity," even though he was never criminally charged with terrorism.
Federal authorities used the allegation to attempt to deport Megahed.
The Muslim community, the Megahed family and the jurors at his trial have said they believe that Megahed is a victim of profiling.
Megahed told CNN in June that he feels "this is double jeopardy because the same allegations here are the same allegations that was there, in the court, in the trial."
This is not the first time the government has gone to immigration court as a last resort after failing to win a criminal prosecution.
"The government doesn't use this a lot, but I think this is an arrow in the quiver that needs to stay because there are those cases where the government needs to do everything in its power to keep us safe from some of those same individuals," former U.S. Attorney Guy Lewis said.
"In one context, the real question is, are you going to jail for a long period of time. The other context is, are you going to get to live among us," Lewis said.
Megahed's father, however, called the case an example of pure discrimination against Muslims.
"They didn't want us to live here. And because he wins the case, they want to destroy him completely," Samir Megahed said.
CNN's Rich Phillips contributed to this report.
All About U.S. Immigration and Customs Enforcement • Youssef Megahed • Islam