HIALEAH, Fla. — A gunman shot and killed six of his neighbors and held two other people hostage during an eight-hour standoff before a police SWAT team raided an apartment and fatally shot him early Saturday morning, law enforcement officials said.

The two hostages were rescued unharmed.

The gunman, identified as Pedro Vargas, 43, lived in the apartment complex northwest of downtown Miami with his mother, according to neighbors. They said Mr. Vargas had been embroiled in a dispute with the building’s managers, a husband-and-wife team who were among the dead.

The police said the episode began around 6:30 p.m. Friday when Mr. Vargas set fire to his apartment and began roaming the complex, shooting at his neighbors. By the time the authorities realized they were facing something more than a fire and police officers responded, the six victims were probably already dead, a police spokesman said.

“Vargas ran throughout the building, firing shots at random in a very irrational fashion,” Detective Carl Zogby of the Hialeah Police Department told reporters.