GLEN ROCK, N.J. — As he hoisted the rainbow flag skyward, Officer Matt Stanislao felt a sense of the surreal as it joined the American flag atop the flagpole and fluttered in the breeze. He looked dazed as the crowd of elected officials and community leaders gathered in the park for the town’s first official gay pride celebration applauded him, their honored guest.

Less than a month earlier, he had been jobless, fired more than two years ago from the police force in this northern New Jersey town after a long-term campaign of what he says were homophobic attacks within the department. It was part of a culture that, according to a lawsuit he filed in 2014 against the Borough of Glen Rock, extended from the rank and file all the way to the former chief.

In April, the town reached a $750,000 settlement with him that also included providing back pay and benefits for all the time since he had been fired. And Officer Matt, as he is known to the teenagers to whom he taught antidrug classes in local schools and the children he read to at the local library, got what he truly wanted: his job back.

“There are no words,” Officer Stanislao said as he stood in the park that afternoon in his dress blues, having been transformed from pariah to honoree.

Across the street, the windows of the red brick Police Headquarters looked out over the flagpole, the scene of a colorful coda to a dark episode that shook not just the tight-knit police force but the sense of self of a small village now waking up to its own ugly undercurrents — and vowing to do better.

“Like a lot of small towns, Glen Rock can be a little bit in a bubble, and I think you are witnessing it bursting,” said the mayor, Bruce Packer, a Democrat who was elected last year. The settlement with Officer Stanislao does not require the town to admit any wrongdoing. But Mr. Packer said that the borough, with a population of about 12,000 that is 85 percent white, and its institutions had been insulated from changing mores by its homogeneity.

“Small towns like Glen Rock try to hold on to a myth,” the mayor said. “You really can’t live on a myth. You have to address reality. And I think that’s what we’re doing.”

Image

Officer Stanislao, center, with Chief Dean Ackermann, left, and Sgt. Christopher McInerney at the Police Headquarters last month.CreditBryan Anselm for The New York Times

Officer Stanislao, the son of a police officer and a legal secretary, said he was inspired to become an officer by his father and school visits from a Drug Abuse Resistance Education, or D.A.R.E., officer — one of the first people he believed he could trust with anything. “I never even drank or did drugs, but he told me I was safe, and I believed him,” he said. He grew up wanting to be that rock for other young people.

But almost immediately upon entering the police force in 2004 he became the target of derisive gossip after officers saw him out on a date with the man he later married. Though Officer Stanislao did not disclose his sexual orientation, his lawsuit includes depositions from department colleagues detailing how rumors of his homosexuality were bandied about the department. Homophobia, the suit alleges, created a hostile work environment, and led to acts of retaliation after Officer Stanislao complained of his treatment.

One of the cruelest episodes was in 2011, Officer Stanislao said in an interview, when he approached a police captain, Jon Miller, to ask for time off to undergo surgery to his throat, adding that there was a risk he would not survive the operation. Captain Miller responded, “I guess you won’t have a gag reflex anymore, huh?” according to the complaint.

Mr. Miller, who is now retired, could not be reached for comment. In a deposition, he said his reaction had been taken out of context.

Beyond the biased treatment that Officer Stanislao said he was suffering, the Glen Rock Police Department seemed to be spiraling out of control a few years ago, whiplashing the town with a series of firings of well-liked officers — including Officer Stanislao. Several sued and were ultimately reinstated after public outcry. A rash of retirements among top officials took place around the same time.

On May 5, Officer Stanislao got his job back.

The behavior of his fellow officers “encumbered his ability to do his job every day because the leadership of that department just couldn’t accept a gay man in their ranks,” said Officer Stanislao’s lawyer, Charles J. Sciarra. “The impact was devastating, but he had two choices — curl up and go away or straighten up and start fighting.”

The chief at the time, Fred Stahman, who is cited in the lawsuit, did not respond to a message left at his home. Daniel Dour, who is also mentioned and was a sergeant and briefly chief, declined to comment. Another person who appears in the suit, Detective Sergeant Eric Reamy, is in prison, serving three years after pleading guilty to child endangerment and other crimes, after sending sexually explicit text messages to a minor girl.

Officer Stanislao said that the entrenched homophobia he had encountered in the department did not surprise him.

Image

Barney Frank, the former congressman from Massachusetts, at Glen Rock’s pride celebration this month. “In a town like this, this is a major example of the progress we are making,” he said.CreditFred R. Conrad for The New York Times

“There is this idea that when benchmarks are made on a national level, whether it be ‘Don’t Ask Don’t Tell’ or marriage equality, that somehow it has this overarching effect on a micro level,” he said. “That is a misconception which really should be addressed — because it’s always there.”

The current police chief, Dean Ackermann, who was promoted in September, has taken on the task of recalibrating his department. Mandatory sensitivity training for officers has been expanded this year to include sexual orientation; town employees and officials must also now receive the training. Similar instruction is part of the curriculum at the Bergen County Law and Public Safety Institute, the local police academy.

“In public safety, it is often tradition unimpeded by progress,” Chief Ackermann said. “But society and people have changed, and you have to be cognizant of that. Police officers are often unaccepting of change — even good change.”

After being fired, Officer Stanislao enrolled in the Columbia University School of Social Work. He received a master’s degree a few days after he rejoined the police department.

During his studies, he met Barney Frank, the former Democratic congressman from Massachusetts, who took an interest in his case. At the gay pride event in Glen Rock, Mr. Frank, 77, looked on as Officer Stanislao raised the gay pride flag. Coincidentally the celebration took place 30 years to the day after Mr. Frank publicly came out, he said. Mr. Frank is now a speaker advocating gay and liberal causes.

Afterward he sat in the sun on a park bench beside Officer Stanislao. “Celebrating gay pride in the city is fun, but it’s not a big deal anymore,’’ Mr. Frank said. “In a town like this, this is a major example of the progress we are making.”

Though the officer’s reinstatement did not involve an acknowledgment of anti-homosexual bias by the borough, the day was still a win, Mr. Frank said. “Nobody admitted it, but they had Officer Stanislao here raising the gay flag.” he said. “I’ll take that.”

“I’ll take that too,” Officer Stanislao said. He stood up from the bench and went back to work.

A version of this article appears in print on , on Page A20 of the New York edition with the headline: Gay Pride Ceremony Honors A Police Officer’s Ordeal In a Changing Community. Order Reprints | Today’s Paper | Subscribe