(Reuters) - Baltimore police officers fatally shot a man armed with a semi-automatic rifle who opened fire on them but the incident was not believed to be an ambush of law enforcement, police said on Friday.

The incident late on Thursday came a week after five police officers were killed during a Dallas protest by a sniper who said he wanted to "kill white people" because of police killings of black people in the United States.

The exchange of fire on the mostly African-American city's west side took place after four plainclothes detectives on patrol heard gunshots at about 9:30 p.m. on Thursday, Police Chief Kevin Davis said.

The officers responded to an apartment building where they found a man in a parking lot firing an AR-15-style weapon, he said at a news conference.

The man fired several .223-caliber rounds at the officers from about 50 yards. Two of the officers returned fire and struck the man, who later died.

"Based on the information we have right now, we do not believe this was an ambush," Davis said.

Davis identified the gunman as Dayten Harper, 33. He had a lengthy criminal record and lived in the area, he said.

There is no indication that the incident was related to a warning from the Federal Bureau of Investigation on Thursday that local gangs were targeting police, Davis said.

He said it was still unclear why Harper had been firing the weapon. The rifle was recovered in a wooded area, where Harper had tossed it.

Davis said one of the officers had a body-worn camera but did not have time to turn it on. Surveillance cameras did not capture the incident, he said.

The gunfire came as Baltimore awaits a decision in the trial of a police officer charged in the 2015 death of black detainee Freddie Gray, which triggered protests and rioting.

(Reporting by Ian Simpson in Washington and Brendan O'Brien in Milwaukee; Editing by Tom Heneghan and Alistair Bell)