At least three policemen and five insurgents were killed during the battle to regain control.
The Taliban has claimed responsibility for the attack, which began when a suicide bomb was detonated shortly before dawn.
It is the second such major attack in Kabul in a week.
Last week, the National Directorate of Security, in the city centre, was attacked, and four guards working for the intelligence services were killed.
The Taliban, who have hit a number of Kabul targets recently, claimed responsibility for Monday’s attack in a text message sent to media organisations.
The siege began when two bombs were detonated – one a suicide bomb and the second a car bomb – before armed men stormed the building in the west of the capital.
It took Afghan forces nine hours to dislodge them, Interior Ministry spokesman Sediq Sediqi told the Associated Press.
Kabul police chief Gen Ayoub Salangi told Agence France-Presse that the operation had taken a long time partly because police did not want to damage documents held inside the building.
“It’s over. The last two terrorists are dead and they were not even given the chance to detonate their suicide vests,” he said.
The militants were armed with heavy machine guns and rocket-propelled grenades and tossed hand grenades out of windows of the four-storey building.
The incident caused traffic gridlock in certain areas of Kabul – where busy intersections are controlled by police rather than traffic lights – as officers were unable to get to work.