NEW YORK -- A Muslim police officer, who sued the New York Police Department over a policy limiting beards allowed for religious reasons, would face irreparable harm without protection from the courts, a judge said Wednesday as he ordered the city to reinstate the officer's pay and benefits a day after he was suspended and escorted from police headquarters.

U.S. District Judge P. Kevin Castel criticized the city repeatedly before finding it was probable Officer Masood Syed would succeed and issuing a temporary restraining order that will remain in place until a July 8 hearing.

The department says it bans beards as a safety measure so gas masks fit tightly across faces but allows beards for religious reasons up to 1 millimeter in length.

Get Breaking News Delivered to Your Inbox

The judge, who is bearded, said 1 millimeter of hair exists when a man is unshaven for a day or two. He said it seemed the policy is enforced haphazardly.

"I am very relieved," Syed said afterward as he celebrated his 32nd birthday with a half dozen fellow officers supporting him in the Manhattan court.

Brooklyn patrol officer Rohail Kahlid, who has a beard about a half-an-inch long, agreed that enforcement can be arbitrary.

"It becomes an issue on and off. It depends on who's the boss," Khalid told the New York Post.

Syed said two uniformed supervisors escorted him from Manhattan NYPD headquarters where he works as a law clerk to administrative judges, sometimes preparing legal documents for the police commissioner about disciplinary issues.

"It was extremely humiliating," Syed said. "I felt insulted, frankly."

New York Police Department Officer Masood Syed, a practicing Muslim, leaves Manhattan federal court in New York Wednesday, June 22, 2016. Larry Neumeister, AP

Syed said he has maintained a 1-inch beard for most of his 10-year career, only occasionally drawing critical comments. He said he hoped his lawsuit, which seeks class-action status, would help over 100 police employees who find the religious exception to the department's no-beard policy insufficient because they say the length of the beard allowed is not reasonable.

The lawsuit seeks unspecified financial damages and a court order banning the NYPD from enforcing its beard policy until it provides reasonable religious accommodations.

City attorney Michael Fleming said the beard ban was necessary. Besides for religious reasons, exceptions exist for undercover duties and medical conditions. Occupational Health and Safety Administration regulations require annual fit-testing and prohibit respirators for employees with facial hair.

The judge became impatient as he questioned Fleming about the policy and the department's failure to respond to a December 2015 request by 37 police officers for an exception to the no-beard policy.

"Let's play 20 questions," Castel said at one point.

"The court is troubled by the fact it cannot get an answer," he said at another.

Attorney Joshua Moskovitz, representing Syed, told Castel the department's policy violates the First Amendment and threatens Syed's job, his retirement benefits and his reputation. He said the department took Syed's shield and weapon Tuesday before he "was escorted out in front of his friends and colleagues."

As he announced his ruling, Castel referenced a November 2013 ruling by the late-Judge Harold Baer Jr. after an Orthodox Jewish officer sued the department after it forced him out because he insisted on growing a 1-inch beard.

In that case, the city argued that the 1-millimeter rule was consistent with counterterrorism and emergency preparedness goals, but Baer found the rule was applied unevenly and was too broad to satisfy the law.