Mayor Bill de Blasio sat in the front pew at St. Patrick’s Cathedral on Sunday, head bowed at times, with his wife on his left and the police commissioner on his right.

The crowd had come, Cardinal Timothy M. Dolan said, to mourn the two officers killed in an ambush in Brooklyn on Saturday. It had come to pray for their families and their “brothers and sisters in uniform.”

He went on.

“We pray for our leaders as well,” the cardinal said, looking toward the mayor’s row. “You’ve done what so many New Yorkers do in times of trial. You’ve come to St. Patrick’s.”

At the helm of a grieving New York, still raw from weeks of protests amid a national reckoning over law enforcement and race, Mr. de Blasio faces his biggest test yet.