A 22-year-old Queens man was charged on Tuesday with attempting to provide material support to the Islamic State, after he flew from New York to Saudi Arabia this spring with the intention, federal prosecutors said, of entering Syria to join the terrorist group.

The man, Parveg Ahmed, flew to Saudi Arabia in early June with a friend, the prosecutors said, ostensibly to celebrate the fasting holiday of Ramadan. But according to a criminal complaint unsealed in Federal District Court in Brooklyn, the two men actually planned to travel to Syria to fight alongside the Islamic State, or ISIS. They were stopped from entering by officials of an unidentified country that borders Syria, and were taken into custody.

Though it remains unclear how or why the investigation started, prosecutors said that in October 2014, Mr. Ahmed wrote on one of his social media accounts that “jihadis” are “Muslims who fight to establish the Sharia IN THEIR OWN LANDS,” and that Americans “are the real terrorists.”

A few months later, according to the complaint, he posted a message on Twitter saying that “the war on the Islamic State is a war of the ideologies” and sent a private Twitter message to a friend declaring that the only country still working in reverence “to the Almighty is the Islamic State.”