15-year-old "had plenty of opportunities to lower the gun and listen to the officers' orders, and he didn't want to," Interim Police Chief Orlando Rodriguez said.
Shortly before the confrontation, the boy had walked into a classroom and punched a random boy in the nose for no apparent reason. Police did not know why he pulled out the weapon.
"We think it looks like this was a way to bring attention to himself," the police chief said.
The boy, identified as Jaime Gonzalez, did not threaten students or teachers, and no one else was hurt.
The officers "took the necessary action to protect themselves and the other kids," Rodriguez said. There weren't many others in the hall at the time, but "they had every right to take the action that they took."
Authorities declined to share what the boy said before being shot.
The shooting happened as classes were about to begin at Cummings Middle School in Brownsville. Teachers locked classroom doors and turned off lights, and some frightened students dove under their desks. They could hear police charge down the hallway and shout for Gonzalez to drop the weapon, followed by several shots.
Two officers fired three shots, hitting Gonzalez at least twice, police said.
The boy's godmother, Norma Leticia Navarro, said she couldn't imagine why he would have brought a gun to school.
"I wish I could ask him why he did that, `Why did you put yourself in that position?'"
She said she understood that police were doing their job, but she wondered if other steps could have been taken.
"Jaime was not a bad kid," she said. "I'm not saying he was perfect or an angel, but he was a very giving person."
David A. Dusenbury, a retired deputy police chief in Long Beach, Calif., who now consults on police tactics, said the officers were probably justified in their actions.
If the boy was raising the gun as if to fire at someone, "then it's unfortunate, but the officer certainly would have the right under the law to use deadly force."
Administrators said the school would be closed Thursday but that students could attend classes at a nearby elementary school if they wished.
Superintendent Carl Montoya remembered Gonzalez as "a very positive young man."
"He did music. He worked well with everybody. Just something unfortunately happened today that caused his behavior to go the way it went. So I don't know."
Gina Rangel was in her first-period class in the gym when the school was locked down. She said friends who were closer to the confrontation heard the boy threaten to kill everyone.
Her mother, Irma Rangel, said she was worried about the school's safety "because if this happened once, kids imitate."
Brownsville, on Texas' southern tip, is beset by spillover violence from Mexico's drug war. As word of the shooting spread through the city, frantic parents rushed to reach their children.
Those who got there early were able to retrieve their kids, but some who arrived later found the street outside the school lined with squad cars and blocked off.
Two hours later, dozens of frustrated parents and relatives flooded out of the park pavilion without their children after school officials announced that all remaining children had been bused to a high school and could be picked up there.