AUBURN, Mass.  —The man suspected of killing a Massachusetts police officer during a weekend traffic stop had a lengthy criminal record and had been released from a maximum-security prison in 2013, officials said Monday.

The suspect, 35-year-old Jorge Zambrano, was fatally shot by police late Sunday after he burst out of a bedroom closet and opened fire on officers inside a duplex apartment in Oxford, investigators said. Oxford is about 7 miles from Auburn, where police Officer Ronald Tarentino was fatally shot early Sunday morning.

A state trooper was shot in the shoulder by Zambrano, and officers returned fire, state police Col. Richard McKeon said.

The trooper, an 18-year veteran of the force and former U.S. Navy Seal, underwent surgery late Sunday night and was recovering in the hospital. The trooper’s name wasn’t released.

Zambrano died at a hospital.

State officials said Zambrano had been released from the Souza-Baranowski Correctional Center in Shirley on Nov. 1, 2013, after serving time on a list of charges, including cocaine trafficking, two counts of assault and battery on a police officer and two counts of resisting arrest.

The violent bedroom encounter came nearly 18 hours after Tarentino pulled over Zambrano at about 12:30 a.m. in Auburn, because the license plate on the SUV he was driving was not registered to that vehicle, said Paul Jarvey, a spokesman for Worcester District Attorney Joseph Early Jr.

Zambrano fatally shot the 42-year-old officer and then fled, investigators said.

Officials later learned that he was at the Oxford duplex and spotted what they believed was his vehicle parked behind the building. Jarvey said Zambrano knew someone who lives at the duplex.

Tarentino had been with the Auburn police force for two years, and before that worked with the Leicester Police Department in his hometown. He leaves behind a wife and three children.

State and local police officers lined up outside of the hospital Sunday as a police vehicle, escorted by a procession, took Tarentino’s body to the state medical examiner’s office in Boston, where the vehicle was met by another large contingent of officers.

Outside the Auburn police station, the American flag was lowered to half-staff. The town’s residents left bouquets of flowers and miniature American flags piled at the bottom of a stone monument dedicated to law enforcement officers killed in the line of duty.