The shooting occurred at Camp Liberty, a sprawling U.S. base on the western edge of Baghdad near the city's international airport and adjacent to another facility where President Barack Obama visited last month.
White House spokesman Robert Gibbs called the shooting a "terrible tragedy" and said Obama planned to meet with Defense Secretary Gates later in the day to discuss the matter. Gibbs said the president's heart goes out to the victims' families and wants to know what happened.
A brief U.S. statement said the soldier "suspected of being involved with the shooting" was in custody but gave no further details. A senior military official in Washington said three others were wounded, but the U.S. military in Baghdad said nobody else was hurt.
In Washington, Pentagon officials said the shooting happened at a stress clinic, where troops can go for help with the stresses of combat or personal issues. It was unclear whether those killed were workers at the clinic or were there for counseling. No details were released about the gunman or what might have provoked the shooting.
"Anytime we lose one of our own, it affects us all," U.S. military spokesman Col. John Robinson said in Baghdad. "Our hearts go out to the families and friends of all the service members involved in this terrible tragedy."
Separately, the military announced Monday that a U.S. soldier was also killed a day earlier when a roadside bomb exploded near his vehicle in Basra province of southern Baghdad.
The death toll from the Monday shooting was the highest for U.S. personnel in a single attack since April 10, when a suicide truck driver killed five American soldiers with a blast near a police headquarters in Mosul.
Attacks on officers and sergeants, known as fraggings, were not uncommon during the Vietnam war as morale in the ranks sank. However, such attacks are believed to be rare in the wars in Iraq and Afghanistan.
In 2005, Army Sgt. Hasan Akbar was sentenced to death for killing two officers in Kuwait just before the U.S.-led invasion of Iraq in 2003.
In June 2005, an Army captain and lieutenant were killed when an anti-personnel mine detonated in the window of their room at the U.S. base in Tikrit. National Guard Staff Sgt. Alberto Martinez was acquitted in the blast.
Additionally, there have been several incidents recently when gunmen dressed as Iraqi soldiers have opened fire on American troops, including an attack in the northern city of Mosul on May 2 when two soldiers and the gunman were killed.
Also Monday, a senior Iraqi traffic officer was assassinated on his way to work in Baghdad. It was the second attack on a high-ranking traffic police officer in the capital in as many days.
A car cut off Brig. Gen. Abdul-Hussein al-Kadhoumi as he drove through a central square in the capital and a second vehicle pulled up alongside and riddled him with bullets, police said, citing witnesses. Al-Kadhoumi was director of operations for the traffic authority.
The gunmen were armed with pistols equipped with silencers, the police added on condition of anonymity because they were not authorized to speak to the media.
Incidents involving gunmen armed with sophisticated weapons, including silencers, have been on the rise since a string of high-profile robberies in April.
Associated Press Writer Pauline Jelinek contributed to this report from Washington.