A student pilot from Egypt has agreed to leave the US after posting on Facebook that the world would thank him if he killed Donald Trump.

Emadeldin Elsayed, 23, was not charged with a crime but the US authorities sought to deport him, his attorney, Hani Bushra, said.

Immigration authorities said they would allow him to return home voluntarily as long as he departed by 5 July.

Mr Elsayed is being held in a jail in California after his visa was revoked.

Mr Bushra said on Monday that Mr Elsayed’s detention was illegal.

“He’s being detained, I think, primarily because he’s a Muslim and he’s a Middle Easterner,” Mr Bushra said. “This kid is going to become a poster boy for hating America.”

Republican US presidential candidate Donald Trump reacts to the results of Super Tuesday primary and caucus voting during a news conference in Palm Beach, Florida March 1, 2016.
Mr Trump is leading the race for the Republican nomination for the presidency

US Secret Service agents interviewed Mr Elsayed in early February after he posted a photo of Mr Trump on Facebook and wrote he was willing to serve a life sentence for killing the billionaire, and the world would thank him, Mr Bushra said.

Mr Elsayed had said earlier that he never intended to hurt anyone. He said he wrote the post because of Mr Trump’s comments about Muslims.

Mr Trump, who is leading the race for the Republican nomination for the presidency, has promised a crackdown on immigration. He has vowed to build a wall along the entire Mexican border and called for a temporary ban on Muslims entering the country.