President Uhuru Kenyatta vowed to crack down on terrorists, and on Thursday, he abruptly ordered 10,000 police recruits to report to duty despite a court order saying they may have been selected through corrupt practices. Human rights observers have said that Mr. Kenyatta is trying to consolidate control and using the fear of terrorism to quash civil rights.

On Thursday, panicked students streamed out of the besieged university with chilling stories of witnessing their classmates being killed. Augustine Alanga, 21, an economics student, said he had been asleep in his dormitory when the shooting began. He said he bolted from his room without stopping to put on his shoes, cutting his feet as he sprinted barefoot across the campus and into a nearby forest.

“When I looked back, I saw them,” Mr. Alanga recalled. “There were five or six of them. They were masked. And they were shooting live rounds.”

Image Students took shelter in a vehicle after gunmen attacked the Garissa University College. Credit... Associated Press

The authorities said the attack began when the gunmen killed two guards standing by the main gate of the university.

Police officers nearby “heard the gunshots and responded swiftly, and engaged the gunmen in a fierce shootout; however, the attackers retreated and gained entry into the hostels,” a police statement said. “Security agencies arrived and are currently engaged in an elaborate process of flushing out the gunmen.”

The police surrounded and sealed off the campus, and by 11 a.m., three of the college’s four student dormitories had been evacuated, while “the attackers have been cornered in one hostel,” the Interior Ministry said on Twitter. It was unclear whether any bystanders had been killed by the security forces.