A suicide bomber on a motorbike carried out the attack on a joint patrol of Afghan and coalition forces at about 1:30 p.m. in the Bajawri area of Bagram district, said Waheed Sediqi, a spokesman for the governor of Parwan province.
Two other U.S. service members and an American contractor were also wounded, officials said.
The area is close to the U.S. base in the Bagram district of northern Parwan province.
Carter expressed his condolences to the Americans' families.
"As I saw firsthand during my visit to Afghanistan last Friday, our troops are working diligently alongside our Afghan partners to build a brighter future for the Afghan people," Carter said in a statement. "Their dedicated efforts will continue despite this tragic event."
White House press secretary Josh Earnest in a statement condemned the "cowardly attack," and said the United States "will continue to work together to promote peace and stability in Afghanistan."
The Taliban claimed responsibility for the attack.
The Pentagon did not immediately release the names of those killed. New York City Police Commissioner William Bratton said one of them was Joseph Lemm, a police detective and National Guardsman.
"Detective Joseph Lemm epitomized the selflessness we can only strive for: putting his county and city first," Brattons said. "Detective Lemm was a 15-year veteran of the NYPD and was promoted to detective in January of 2014, serving in the Bronx Warrant Squad.
"As a member of the NYPD, Detective Lemm was deployed three times, twice to Afghanistan and once to Iraq. Detective Lemm leaves behind a wife and two children. Tonight, we grieve and we remember this selfless public servant who dedicated his life to protecting others."
The attack remains under investigation, said U.S. Col. Michael Lawhorn, a spokesman for NATO's Operation Resolute Support.
The six people killed Monday bring the tally of NATO service members who have died in Afghanistan this year to 25. The death toll has steadily declined year by year since reaching a peak of 711 in 2010.
The number of U.S. troops in Afghanistan stands at just under 10,000, down from its peak of 100,000 in 2010.
According to NATO, the operation was launched in January
"to provide further training, advice and assistance for the Afghan security forces and institutions." The mission involves 12,000 personnel from NATO and 14 partner nations.
Its central hub is in Bagram/Kabul with "spokes" in Mazar-e Sharif, Herat, Kandahar and Laghman, NATO says.