Recruits at the New York Police Department academy graduation ceremony at Madison Square Garden on Dec. 29. (Peter Foley/European Pressphoto Agency)

The New York Police Department has agreed to settle a pair of federal lawsuits that claimed Muslims were the target of baseless surveillance and investigations because of their religion.

As part of the agreement with civil rights groups that was disclosed Thursday in federal court in Manhattan, the NYPD said it would strengthen the oversight of terrorism investigations and ensure that it follows a series of guidelines to avoid religious profiling.

The NYPD’s decision to settle the case is a victory for Muslims in the city who asserted that the department’s intelligence division had gone too far in its efforts to identify potential terrorists after the attacks of Sept. 11, 2001.

“It’s a win for Muslims, but I think it is a win for everyone in New York, because any time a group is targeted, all groups are vulnerable,” said Jethro Eisenstein, one of the civil rights lawyers who argued the case.

As part of the agreement, the NYPD, the country’s largest police force, admitted no wrongdoing but agreed to pay about $2 million in attorney fees.

John Miller, the force’s deputy commissioner for intelligence and counterterrorism, said investigators would not lose any operational capabilities with the settlement.

“We are not hampered in either starting, continuing or closing any investigation in any way that’s new,” he said, adding that new measures codify existing practices and should assure people that the NYPD is opening legitimate cases.

The new protections will include a civilian representative — a lawyer with federal security clearances who has never worked for the NYPD — with the ability to review terrorism investigations.

The lawyer will be appointed by the mayor and serve a five-year term. That person is empowered to go to the police commissioner and then, if not satisfied, a federal judge if there are concerns that the police have overstepped their bounds.

Under the agreed guidelines, there will be mandatory reviews and specific limits to the lengths of terrorism investigations unless there is a legitimate law enforcement reason to continue them.

The NYPD will be required to “consider the impact of intrusive investigative techniques and to use the least intrusive means consistent with the needs of the investigation,” according to court documents.

“In America, we have the right to stand up and speak out in response to unfairness and injustice, just as throughout this country’s history, other minorities have done the same thing and secured their rights,” said Imam Hamid Hassan Raza, the lead plaintiff in one of the lawsuits. “We believe we have made important progress with this settlement, not only for New York Muslim communities but for other minorities in New York and beyond,” Raza added.

The new measures will be based on guidelines the department had already agreed to in 1985, when it entered a consent decree over alleged spying on political organizations.

The “Handschu Guidelines” — named after the lead plaintiff in the 1985 lawsuit — govern how the department is supposed to carry out investigations that bump up against political or religious activity.

The NYPD intelligence division once had civilian oversight as part of the guidelines, but David Cohen, a former senior CIA officer hired to run the division in 2003, argued the procedures would hamper terrorism investigations.

A federal judge overseeing the Handschu consent decree agreed with Cohen and revised the guidelines. The same judge will have to approve the new settlement.

In 2011, the Associated Press revealed that the NYPD had been gathering extensive information on the city’s Muslims using plainclothes detectives. And the use of informants, as well as undercover officers, has been a major concern for Muslims in New York.

After the AP revelations, the Handschu lawyers went to federal court asserting the NYPD had violated the guidelines. The American Civil Liberties Union, the Creating Law Enforcement Accountability & Responsibility project at City University of New York School of Law, and the New York Civil Liberties Union also sued the NYPD in 2013.

In the lawsuit, they accused the city of violating the rights of their clients under the First and 14th amendments.

“This settlement sends a forceful message that bias-based policing is unlawful, harmful and unnecessary,” said Hina Shamsi, director of the ACLU’s National Security Project.

The NYPD began joint settlement talks with the Handschu lawyers and others in the case in August 2014 after a judge ordered that plaintiffs could access electronic communications involving the department’s field-level personnel, including those who handled informants and undercover officers.

Plainclothes detectives monitoring Muslims were part of the secret Demographics Unit within the intelligence division, and the NYPD’s top spokesman at the time, Paul Browne, initially denied the unit existed.

The group’s name was later changed to the Zone Assessment Unit, and then it was disbanded under Police Commissioner William Bratton. Handschu lawyers argued the unit’s secret files on Muslim communities in New York violated the guidelines.

As part of the litigation, plaintiff attorneys pointed to leaked documents that showed the NYPD had been investigating certain individuals for years without bringing charges. The NYPD also designated mosques as terrorism enterprises, monitoring them for years with informants and undercover officers. The NYPD also used license plate readers and video cameras to watch houses of worship.

None of those investigations resulted in charges.

Miller said there was a “learning curve” after 9/11 as the division “got into the business of counterterrorism.”

The city’s Muslim spying programs were developed, in part, by CIA officers who were assigned to the police department on an ad hoc basis or had retired and gone to work for the intelligence division.

Former police commissioner Raymond Kelly, who oversaw the expansion of the department’s counterterrorism operations in the wake of 9/11, has insisted in speeches that there was adequate oversight in place and that the department had done nothing wrong.

The department also agreed to distance itself from a controversial internal study it published in 2007 called “Radicalization in the West: The Homegrown Threat.” As part of the settlement, the NYPD agreed to take the study off its website. The report was criticized for “faulty conclusions” that would lead to “racial and religious profiling.”

The NYPD’s legal problems regarding its Muslim spying programs are not over. It still faces a lawsuit in New Jersey over its surveillance activities in that state.