GARISSA, Kenya -- Al-Shabab gunmen rampaged through a university in northeastern Kenya at dawn Thursday, killing 147 people in the group's deadliest attack in the East African country. Four militants were slain by security forces to end the siege just after dusk.

The masked attackers armed with AK-47s singled out non-Muslim students at Garissa University College and then gunned them down without mercy, survivors said. Others ran for their lives with bullets whistling through the air.

The men took dozens of hostages in a dormitory for several hours as they battled troops and police before the operation was ended after about 13 hours, witnesses said.

Al-Shabab spokesman Ali Mohamud Rage said fighters from the Somalia-based extremist group were responsible for the bloodshed. The al-Qaida-linked group has been blamed for a series of attacks in Kenya, including the siege at the Westgate Mall in Nairobi in 2013 that killed 67 people, as well as other violence in the north.

Most of the 147 dead were students, but the two security guards, one policeman and one soldier also were killed in the attack, said Interior Minister Joseph Nkaissery.

At least 79 people were wounded at the school 145 kilometres (90 miles) from the Somali border, Nkaissery said. A dusk-to-dawn curfew was ordered in Garissa and three nearby counties.

One suspected extremist was arrested as he tried to flee, Nkaissery told a news conference in Nairobi.

Police identified a possible mastermind of the attack as Mohammed Mohamud, who is alleged to lead al-Shabab's cross-border raids into Kenya, and they posted a $220,000 bounty for him. Also known by the names Dulyadin and Gamadhere, he was a teacher at an Islamic religious school, or madrassa, and claimed responsibility for a bus attack in Makka, Kenya, in November that killed 28 people.

One of the survivors of Thursday's attack, Collins Wetangula, told The Associated Press he was preparing to take a shower when he heard gunshots coming from Tana dorm, which hosts both men and women, 150 metres (yards) away. The campus has six dorms and at least 887 students, he said.

When he heard the gunshots, he locked himself and three roommates in their room, said Wetangula, who is vice chairman of the university's student union.

"All I could hear were footsteps and gunshots. Nobody was screaming because they thought this would lead the gunmen to know where they are," he said.

He added: "The gunmen were saying, 'Sisi ni al-Shabab,"' -- Swahili for "We are al-Shabab."

He heard the attackers arrive at his dormitory, open the doors and ask if the people who had hidden inside were Muslims or Christians.

"If you were a Christian, you were shot on the spot," he said. "With each blast of the gun, I thought I was going to die."

The gunmen then started shooting rapidly, as if exchanging fire, Wetangula said.

"The next thing, we saw people in military uniform through the window of the back of our rooms who identified themselves as the Kenyan military," he said. The soldiers took him and around 20 others to safety.

Students of the Garissa University College take shelter in a vehicle after fleeing from an attack by gunmen in Garissa, Kenya, Thursday, April 2, 2015. (AP)

The attack began about 5:30 a.m., as morning prayers were underway at the university mosque, where worshippers were not attacked, said Augustine Alanga, a 21-year-old student.

At least five heavily armed, masked gunmen opened fire outside his dormitory, turning intense almost immediately and setting off panic, he told the AP by telephone.

The shooting kept some students indoors but scores of others fled through barbed-wire fencing around the campus, with the gunmen firing at them, he said.

"I am just now recovering from the pain as I injured myself while trying to escape, Alanga said. I was running barefoot," Alanga said.

As terrified students streamed out of buildings, arriving police officers took cover. Kenya's National Police Service said a "fierce shootout" ensued as police guarded the dorms.

Three of the dorms were evacuated, with the gunmen holed up in a fourth, the National Disaster Operations Center said, and Kenya Defence Forces surrounded the campus.

"I am saddened to inform the nation that early today, terrorists attacked Garissa University College, killed and wounded several people, and have taken others hostage," President Uhuru Kenyatta said in a speech to the nation while the siege was still going on.

President of Kenya, Uhuru Kenyatta, makes a speech to the nation after the attack on Garissa University College on Thursday, April 2, 2015.

Michael Bwana, a 20-year-old student, said he and other survivors tried to call their friends trapped in a dormitory, but their phones were switched off -- either by their owners to keep them from ringing or by the gunmen who have seized them.

"Most of the people still inside there are girls," Bwana said, referring to the dorm where gunmen are believed to be holding an unknown number of captives.

Wetangula, who was rescued by troops, said one soldier instructed a group of students to run and to dive for cover at their command as they ran to safety.

"We started running and bullets were whizzing past our heads, and the soldiers told us to dive," Wetangula said. The soldier told students later that al-Shabab snipers were perched on a three-story dormitory called the Elgon, he said.

Some of the more seriously wounded were flown to Nairobi for treatment, authorities said.

Kenyatta has been under pressure to deal with insecurity caused by a string of attacks by al-Shabab.

In his speech to the country, he said he had directed the police chief to fast-track the training of 10,000 police recruits because Kenya has "suffered unnecessarily due to shortage of security personnel."

Kenya's northern and eastern regions near the Somali border have seen many attacks blamed on al-Shabab. The group has vowed to retaliate against Kenya for dispatching troops to Somalia in 2011 to fight the militants following cross-border attacks.

Last month, al-Shabab claimed responsibility for attacks in Mandera county on the Somali border in which 12 people died. Four of them died in an attack on the convoy of the county's governor.

Police said 312 people have been killed in al-Shabab attacks in Kenya from 2012 to 2014.

Last week, al-Shabab claimed responsibility for a siege at a Mogadishu hotel that left 24 people dead, including six attackers.