The attack at Camp Liberty, the US military's largest base in Iraq, brought the number of American losses since the US-led invasion of Iraq to 4,292.
The shooting was the highest death toll for US military personnel in a single attack since April 10, when a suicide blast killed five US soldiers near a police headquarters in the northern city of Mosul.
There have been a number of previous incidents in which US soldiers have attacked their colleagues in Iraq.
The last such report was on September 14 when US Sergeant Joseph Bozicevich shot dead two of his superiors at a base south of the capital.
Voicing "horror and deep regret" over the deaths, Robert Gates, the US defence secretary, said "... such a tragic loss of life at the hands of our own forces is a cause for great and urgent concern".
Barack Obama, the US president, said he was "saddened" and "shocked" by the incident.