Hundreds of police officers put up a cordon last Tuesday around Ohlauer Straße while clearing around 200 refugees from the Gerhart-Hauptmann-Schule in Kreuzberg.
But around 40 people refused to leave the former school and the police barriers became a magnet for protesters.
PHOTO GALLERY: Police close off area around former school
Under the agreement, brokered with the help of left-leaning veteran Green politician Hans-Christian Ströbele, 75, the former school will be renovated.
The 40 remaining refugees will be given special passes to allow them to leave and re-enter the school at will. Their passes will be checked at the entrance by security staff to avoid an influx of new people.
While they stay in the school, authorities have pledged not to start any legal proceedings of any kind against the refugees.
The renovation work, which according to the signed document will begin on Thursday morning, will start with the clearing out and securing the bottom floors of the school.
During the work the ground floor windows will be boarded up.
Police spokesman Stefan Redlich said he was relieved that the police had not had to use force to clear the school and that nobody had been hurt.
The nine day police operation, involving up to 1,700 police officers from Berlin, Lower Saxony and North Rhine-Westphalia cost over €5 million, Bild newspaper said on Thursday, citing police sources.