Ahmad Ali Hazrat, the chief of the provincial council, said on Friday that the attack was carried out in Dih Bala district of Nangarhar province the previous night.
Hazrat said the Daesh terrorists attacked the posts from three directions and after several hours of fierce battles, 17 soldiers were killed.
General Doulat Waziri, the spokesman for the Afghan Defense Ministry, said army troops killed 21 militants in the battle.
In a separate attack on Friday, Taliban militants assaulted several government security posts in the eastern province of Kunar, killing five police officers.
Police forces, aided by other security personnel, managed to repel the attackers, forcing them to flee. Provincial Governor Waheedullah Kalimzai said a sixth police officer was missing after the attack.
Kunar borders Pakistan and Taliban militants are active in the province.
Pakistan shells base of Daesh
The latest attacks in Afghanistan came as Pakistani security forces shelled Daesh positions in Nangarhar following a terrorist bombing claimed by the Takfiri group that killed at least 80 people, including 20 children, in the city of Sehwan in Sindh province.
Gul Agha Roohani, the provincial police chief of Nangarhar, said Pakistan had fired several artillery rounds early on Friday into Lalpur district, near the Afghan-Pakistan border.
Pakistani officials say the Thursday attack on Lal Shahbaz Qalandar sufi shrine, as well as other recent bombings in Pakistan have been masterminded by militants who hide across the border in Afghanistan.
Islamabad shut down Pakistan's border with Afghanistan following the attack.
Pakistani officials say security operations would continue in the coming days.