(CNN) -- At least three police officers in Oakland, California, were shot and killed Saturday afternoon after a man pulled over for a routine traffic stop opened fire and then battled SWAT officers at a nearby building, police said.

Sgt. Daniel Sakai, from left, Sgt. Mark Dunakin and Sgt. Ervin Romans were killed in the shootings.

The man, a 26-year-old Oakland resident, was later fatally shot in a shootout with a SWAT team in an apartment complex where he hid.

A fourth officer -- also shot -- remained in critical condition, police said at a late-night news conference.

California Gov. Arnold Schwarzenegger's office differed from police, saying four officers had died in the shootings. Watch fellow officers, families grieve »

Authorities discovered the alleged gunman, 26-year-old Lovelle Mixon, had an extensive criminal history. At the time of the shooting, he was in violation of parole for assault with a deadly weapon, police said.

The incident began about 1 p.m. in east Oakland when two motorcycle officers tried to pull over a car for a "fairly routine traffic stop," said Dave Kozicki, the deputy police chief.

Immediately afterward, emergency call dispatchers received reports that two officers were down at the scene and needed attention. They had been shot, Kozicki said. Watch acting police chief describe what happened »

The first victim, Sgt. Mark Dunakin, 40, died, police said. He had been with the force for 18 years. The second officer, John Hege, 41, remained in critical condition.

Don't Miss KGO: Three officers killed, suspect dead

KGO: Three officers killed, suspect dead KTVU: Gunman kills Oakland officers

Soon after the first shooting, police began an intense search for the gunman. An anonymous caller directed authorities to a building on an adjacent street where the gunman was believed to be barricaded, said acting Police Chief Howard Jordan. Watch aerial views of shooting locations »

The gunman opened fire on SWAT officers who entered the apartment, killing two more officers before police returned fire, killing the suspect, Jordan said.

The two police officers killed were Sgt. Ervin Romans, 43, and Sgt. Daniel Sakai, 35. Romans had been with the department since 1996; Sakai since 2000.

A third officer, grazed by a bullet, was treated and released, said police spokesman Jeff Thomason.

"We come together in shock, in grief, in sadness and sorrow at a set of tragic incidences," said Oakland Mayor Ron Dellums.

Schwarzenegger said flags at the state capitol will fly at half-staff to honor the fallen officers.

"This is a tragic day for law enforcement officers everywhere," he said in a statement.

All About Oakland Police Department