Iraqi security forces have freed all the hostages taken by gunmen at a building in Baghdad next the ministry of human rights, officials say.