The gunman was shot dead by other Afghan troops at Pul-e-Charkhi prison, some 20 miles east of Kabul, said Maj. Sheldon Smith, a spokesman for Combined Security Transition Command, which trains Afghan security forces. The shooter also wounded two U.S. soldiers.
The four American soldiers were working as mentors to Afghan troops providing external security for the prison, Smith said. U.S. and Afghan authorities were trying to determine the motive for the attack, Smith said.
The victims were not identified.
"All indications are" that the shooter was a member of the Afghan National Army, the post-Taliban force trained mainly by the American military, Smith said.
Afghan soldiers with their U.S. trainers have been deployed at the prison since the opening last month of a new high-security wing designed to eventually house Afghans released from the U.S. jail for terrorist suspects.
The revamp is supposed to improve security at the jail, which is infamous among Afghans for tales of torture and appalling conditions dating back to communist rule in the 1970s.
Since the U.S.-led invasion in 2001 that topped the Taliban, hundreds of al Qaeda and Taliban suspects have been incarcerated there, some of whom have been involved in a series of deadly riots and breakouts.
Across Afghanistan, violence is escalating as insurgents and the military ramp up operations after a winter lull.
Five police officers died Sunday when a remote-controlled mine hit their convoy in the Chola district of central Ghazni province, Deputy Governor Qazim Allayar said.
In neighboring Bala Buluk district, police opened fire on a suspected suicide bomb attacker, who blew himself up, said Baryalai Khan, spokesman for the Farah police chief. No one else was wounded.
Officials reported the deaths of 15 police officers and 8 suspected insurgents in explosions and fighting around the country over the weekend.
An Associated Press tally has counted at least 43 suicide bomb attacks so far this year, which have killed about 100 people. Nearly half of the suicide attacks have left only the bomber dead.