BERLIN — A simmering dispute between leftist squatters and the authorities in Berlin erupted overnight into what the police on Sunday called “the most aggressive and most violent protest in the past five years.”

What began late Saturday as a peaceful demonstration in the German capital led by groups associated with anti-fascist and far-left movements ended in violence outside a disputed building. Members of the leftist groups and their sympathizers have been protesting the gentrification spreading into Berlin neighborhoods that have been home to squatters for decades.

The Berlin police said they had deployed 1,800 officers, nearly a third of them brought in from outside the capital, and used a helicopter to monitor the protest in an effort to maintain the peace. But as the demonstration progressed into the evening, the number of protesters swelled to 3,500 and some began lobbing cobblestones, firecrackers and glass bottles at the officers, injuring 123, the police said in a statement.

Officers responded with pepper spray and billy clubs to push back surges of demonstrators, some of whom were masked. Eighty-six people were detained on suspicion of disturbing the peace, resisting arrest, causing injuries and the illegal use of explosives, the police said. Most were released.