NAIROBI, Kenya - Masked al-Shabab militants stormed dormitories at a university in northeastern Kenya early Thursday, killing at least 147 people in the worst terror attack on Kenyan soil in nearly two decades, officials said.

More than 500 students were rescued after the Islamist extremists, heavily armed and strapped with explosives, attacked the campus of Garissa University College around 5:30 a.m. local time, shooting some young people and taking others hostage. At least 79 people were injured, according to Kenya's National Disaster Operation Center.

A government spokesman said the siege had ended with four gunmen from the Somalian group being killed 15 hours after it began.

"The gunmen are dead. There were four, they are all dead," said Abdulkadir Sugow, spokesman for the Garissa governor. However, he could not confirm how they were killed. "The fire exchange has now stopped," he said. "The next step is to reconcile, and to analyze the way forward." Security forces have yet to enter the university compound, he said. "Nothing can be ascertained fully," Sugow said.

Outside the university, in the city of Garissa about 90 miles from the border with Somalia, confusion and tension reigned. Scores of students remained unaccounted for. Many jumped through a fence to escape the campus. The gunmen had been holed up in the compound with an unconfirmed number of hostages. When they were shot by police, they exploded "like bombs," said Kenya's interior cabinet secretary, Joseph Nkaissery.

Ogutu Vquee, a student at the university, was sleeping in his dormitory when the gunmen arrived. He said there was indiscriminate shooting of both Muslims and non-Muslims, though there were reports that Muslims were separated from Christians, who were targeted. "When they attacked us, most of us were asleep so we were woken by the gunshots," he said. "I am totally in fear and confusion due to the situation."

Rosalind Mugambi said she fled her dormitory in panic, dodging gunfire. While she ran across the sandy ground into a surrounding field, some of her friends fell. "We saw some bloodstains, and they were shot," she said.

A 19-year-old student in Nairobi, who asked not to be named, said that he transferred from Garissa University to Nairobi after threats of an al-Shabaab attack circulated in December. "Everybody had to go home because there was a lot of tension. Shabaab was saying they were going to attack the school in one week's time, so we went home. It was rumors, but we had to vacate. Everybody had to vacate."

He said the students left in mid-December, missing the end-of-semester exam. "I transferred because of the tension."

The massacre is the worst terrorist attack in Kenya since the bombing of the U.S. Embassy in Nairobi in 1998, which killed 224. An attack on an upscale shopping mall in Nairobi in 2013 left 67 dead and renewed fears that al-Shabab could wage significant operations from its strongholds in neighboring Somalia.

Since the 2013 attack, the U.S. military has maintained a campaign of targeted drone strikes against the leaders of the al-Qaeda-affiliated group. Last month, one such strike killed Adnan Garaar, thought to be behind the mall attack and several others in the region.

An al-Shabab spokesman told Agence France-Presse that the gunmen had been holding Christian hostages. "When our men arrived, they released some of the people, the Muslims, and it is they that alerted the government. We are holding the others hostage," Sheikh Ali Mohamud Rage told AFP by telephone.

Al-Shabab considers Kenya a staunch enemy in part because the country sent troops to Somalia in 2011 to fight the group. Kenyan troops remain there as part of an African Union mission.

U.S. drone strikes had recently appeared to be weakening the group, which has also lost territory within Somalia. American troops have been training African Union soldiers to defeat al-Shabab terrorists. Western sanctions are also thought to have struck a blow to its finances.

Last year, President Obama pointed to the U.S. strategy in Somalia as an example of a successful counterterrorism campaign. He called it a "strategy of taking out terrorists who threaten us, while supporting partners on the front lines."

But Thursday's attack proves that al-Shabab still has the capacity to strike soft targets in the region, and with deadly effect.

The country's border with Somalia is vast and largely unguarded. Attacks on many targets, particularly in rural Kenya, are difficult to prevent. The school's campus had little protection, despite recent security alerts surrounding Kenyan universities.

Kenya is a key American ally in the region - a product of its role in combating terrorism as well as its growing economy and prominence in East African geopolitics.

"The United States stands with the people of Kenya, who will not be intimidated by such cowardly attacks," said press secretary Josh Earnest on Thursday.