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.
He said 19 others were wounded in the attack at the courthouse in Puli Alim, the provincial capital.
The three attackers were shot dead by Afghan security forces, he said.
The Taliban have increasingly targeted the judiciary since the government executed six convicted insurgents last month.
Last week, four Taliban gunmen disguised in women's burqas and wearing military uniforms underneath attacked a court building in eastern Ghazni province, killing five civilians and a policeman, Afghan officials said.