The attack began as the prosecutor, Akram Nejat, was addressing a ceremony to introduce him in his new position, said Nesar Ahmad Abdul Rahimzai, deputy police chief of Logar province.
A statement from the interior ministry said 21 others, including six police officers, were wounded in the attack on the courthouse in Puli Alim, the provincial capital.
The three attackers were shot dead by Afghan security forces, the statement said.
The executions signaled a tougher approach to the Taliban after two years of failed government efforts to revive peace talks.
The UN Assistance Mission in Afghanistan condemned the attack. In a statement, it said that one of those killed was a junior prosecutor and that three judges were among the wounded.
In the statement, the head of UNAMA, Nicholas Haysom, said he urged "authorities to do everything in their power to ensure adequate protection of judicial officials and other civilians seeking access to judicial institutions."
In the capital, Kabul, an Afghan lawmaker was killed in a roadside bombing late Sunday afternoon, said interior ministry spokesman Sediq Sediqqi.
Sher Wali Wardak was a parliamentarian from Kabul, whose brother Farooq Wardak was a former education minister.
Another 11 people were wounded in the attack, which took place near Wardak's house, Sediqqi said.
No group has yet claimed responsibility for the blast in Kabul. Taliban insurgents have been increasing their attacks on security forces and government officials across the country.
