KABUL, Afghanistan — A suicide bomber managed to sneak onto the main American military base in Afghanistan on Saturday and kill four Americans, according to American and Afghan officials.

The attacker entered on foot hidden among a group of workers reporting for duty at Bagram Air Base early in the morning, said Abdul Wahid Sediqi, spokesman for the governor of Parwan Province, where the military base and airfield are. Once inside the base, the attacker detonated explosives, Mr. Sediqi said.

The blast killed two American service members and two American contractors working on the base, Defense Secretary Ashton B. Carter said in a statement on Saturday morning.

The explosion also wounded 16 American service members and a Polish soldier serving with the NATO coalition forces, the statement said.