CHICAGO  A student armed with a handgun held 23 other students and a teacher hostage for several hours on Monday before shooting himself at a high school in Marinette, Wis., which borders the Upper Peninsula of Michigan, the authorities said.

All of the hostages were released unharmed after about five hours. The student, 15, shot himself after police officers entered the room, Chief Jeff Skorik of the Marinette police said. He was taken to a hospital, and his condition was not immediately known.

The officers went in after hearing gunshots, Chief Skorik said.

The episode began after 3 p.m., before school let out, when the student entered a classroom at Marinette High School carrying a handgun, the police said. He kept the class and the teacher in the room as the rest of the school emptied and evening set in.

By late Monday, scores of law enforcement officials and other state authorities from all over eastern Wisconsin had gathered near the school, which was cordoned off, and police negotiators spoke by telephone to the teacher.