The suspect, Scott Michael Greene, was taken into custody in the killings in the Iowa capital Des Moines and its affluent suburb Urbandale, police said. Police said it was unclear what provoked the attacks.
Greene, who has not yet been charged, had a recent run-in with local police after waving a Confederate battle flag, a racially charged symbol, in the crowd while the national anthem was played at the start of a high school football game, Urbandale Police Chief Ross McCarty said.
The shootings represented the latest attacks on police in the United States during a time of intense public debate over race and the criminal justice system in America. Some 52 U.S. police officers have been fatally shot while on duty this year, up 58 percent from the 33 shot dead by this point in 2015, according to the National Law Enforcement Officers Memorial.
Greene is white, as were both officers involved in the shooting, Des Moines police spokesman Paul Parizek told a news conference.
"There's nothing to indicate right now that there was anyone else involved," Parizek said.
Police said they found the first slain officer's body at about 1:06 a.m. (2.06 a.m. ET) in Urbandale, and the second about 20 minutes later about two miles (3 km) away in Des Moines.
The slain officers were identified as Sergeant Anthony Beminio of the Des Moines Police Department and Officer Justin Martin of the Urbandale Police Department.
A police cruiser at the site of the Des Moines shooting could be seen riddled with three bullet holes.
"These officers were ambushed," Parizek said.
INCIDENT AT STADIUM
A 10-minute video posted on YouTube last month by a user calling himself Scott Greene showed an interaction with officers following the flag incident. A voice, apparently Greene's, is heard complaining to police that "African-American people" took the flag from him in the stands and "assaulted" him, and saying he wanted to press charges.
The Confederate flag is a racially charged symbol for its association with the pro-slavery South in the U.S. Civil War.
"There were people in the crowd who felt that was insensitive, and that he should be removed from the stadium," McCarty said.
Police officers shown in the video said he was removed from the stadium because he caused a disturbance in the stands.
"You have to understand, in the current social climate that we're in, when you fly the Confederate flag standing in front of several African-American people, that's going to cause a disturbance, OK, whether you intended to or not," a female officer is heard telling the man in the video.
McCarty told reporters that high school officials had banned Greene from their property following that incident, but had been trying to determine exactly how to enforce the ban given that Greene has a daughter who attends the school.
In Washington, U.S. Attorney General Loretta Lynch urged Americans to avoid jumping to conclusions about the shooter's motive.
"This is a time of particular tension and mistrust between law enforcement and many communities," Lynch said at an event for veterans at the Justice Department. "There is no message in murder. Violence creates nothing. It only destroys."
The killings were the latest violence against U.S. police.
A black Afghan war veteran who said he wanted to "kill white people" fatally shot five police officers in July during a Dallas protest decrying police shootings of black men.
A black Iraq war veteran fatally shot three officers and wounded three others in Baton Rouge, Louisiana, in an ambush later in July.
Those attacks followed fatal shootings by police officers of black men in Minnesota and Louisiana.
Philadelphia police officers have been targeted by a gunman twice this year.
Democratic U.S. presidential candidate Hillary Clinton said on Twitter she was "Heartbroken for the families of two brave officers who were killed in Iowa. There's no justification for this kind of violence."
Republican candidate Donald Trump said on Twitter that he was praying for the slain Iowa officers' families, adding, "An attack on those who keep us safe is an attack on us all."