Three police officers were shot dead in Baton Rouge, Louisiana by an African-American who had served for five years in the Marines.
The suspect, Gavin Long from Kansas City, Missouri, was also killed.
Gavin Long, 29, had posted videos on the internet complaining about police treatment of African-Americans.
Tensions in Baton Rouge have been high since another black man, Alton Sterling, was shot dead by police two weeks ago.
That death – and a second police shooting in Minnesota – sparked protests across the US and triggered a revenge attack by a black army veteran who shot dead five officers in Dallas.
“I’m affiliated with the spirit of justice, nothing more nothing less. I thought my own thoughts, I made my own decisions,” he said.
In a live broadcast from the White House, President Barack Obama called upon all Americans to unite and refrain from divisive language.
“Regardless of motive, the death of these three brave officers underscores the danger that police across the country confront every single day, and we as a nation have to be loud and clear that nothing justifies violence against law enforcement,” said the president.
“Everyone right now focus on words and actions that can unite this country rather than divide it further,” Barack Obama added, as the US begins two weeks of political conventions with Republicans meeting in Cleveland later on July 18.
“We need to temper our words and open our hearts… all of us,” said the president.
A vigil was attended by police officers and members of the public on Sunday evening at Saint John the Baptist Church in Zachary, just north of Baton Rouge.
The incident began on July 17 with shots being fired at a petrol station on Airline Highway in Baton Rouge.
Police received reports of a man with an assault rifle.
Shots were exchanged over a period of more than 15 minutes, leaving three police officers and the suspect dead, with three other officers wounded, one in a critical condition.
The dead officers were named as Montrell Jackson, 32, and Matthew Gerald, 41, of the Baton Rouge police department, and Sheriff’s Deputy Brad Garafola, 45. All three men had families.
Gavin Long, a former Marine, received an honorable discharge, and won several medals while in the military, including one for good conduct.
Louisiana Governor John Bel Edwards told a news conference it was an “absolutely unspeakable, heinous attack.”
Although no other suspects have been identified, police said they were investigating whether the gunman had help from unknown others.
“We are not ready to say he acted alone,” said state police spokesman Major Doug Cain.
Baton Rouge Mayor Kip Holden said he had spoken to White House officials who had offered assistance. He said it was “a defining moment” for community relations.
Kip Holden also told local media the “rhetoric from some people” after the death of Alton Sterling in Baton Rouge may be connected to the shootings, without elaborating who.