A suicide attacker drove a hijacked government jeep that had been loaded with explosives up to the main entrance and detonated it, while at least two other militants wearing police uniforms entered the complex and seized control of a building.
Police say the remaining militants were killed after a gunbattle lasting several hours.
The Jaish-e Mohammad (Army of Mohammad) militant group said it carried out the attack and named the suicide bomber as Pakistani national Wajahat Hussain.
Thirteen people, including the bomber, were killed instantly by the blast and others later died of their injuries.
Eyewitnesses describe seeing people lying in pools of blood after the explosion.
Siege
The Indian authorities say that most members of the state government and assembly had left before the attack, and no lawmakers were killed.
But many members of staff were trapped inside while a battle raged between the army and militants inside the complex.
The BBC's Altaf Hussein, who went to the scene, says gunfire and explosions could be heard from inside the premises.
Police say the militants were lobbing grenades at the police, who responded with gunfire.
Our correspondent says security force officials appeared stunned by the attack, which happened despite heavy security for the autumn session of the state assembly.
Airports 'threatened'
The French news agency AFP quoted Indian officials as saying that Jaish-e Mohammad had recently threatened attacks on Indian airports, all of which have been on high alert.
The militant group was formed by an Islamic cleric, Maulana Masood Azhar, to fight against Indian rule in Kashmir.
Masood Azhar was released from an Indian jail in December 1999, in exchange for hostages on board a hijacked Indian airliner.
In recent days, there has been an upsurge in violence in Kashmir, with as many as 50 people killed in attacks across the territory.
Militant groups have been fighting for the last decade to end Indian rule in Kashmir.
The territory is claimed by both India and Pakistan, who have fought two wars over it since 1947.