A police officer in Flagstaff, Ariz., was killed Saturday afternoon in a shootout while investigating a domestic violence case, the police said.

The gunman then fatally shot himself, the authorities said.

The officer, Tyler Jacob Stewart, 24, was the first Flagstaff officer to die in the line of duty in 13 years, and only the second fatality in the history of the Flagstaff Police Department, according to the authorities.

Officer Stewart was investigating the case on the west side of Flagstaff when a man opened fire; Officer Stewart was struck several times, the police said.

The gunman, who had contacted Officer Stewart after a domestic violence episode at a different location earlier in the day, then shot himself, the police said. He was identified as Robert W. Smith, 28, of Prescott, Ariz., about 90 miles southwest of Flagstaff.