More than 500 students managed to escape, 79 of whom were injured. A fifth gunman has reportedly been arrested.
Image captionThe students have all been evacuated now the siege has ended, Kenyan officials saidEric Wekesa, a student at Garissa, told Reuters he locked himself in his room before eventually fleeing.
"What I managed to hear from them is 'We came to kill or finally be killed.' That's what they said."
"It was horrible, there was shooting everywhere," another student, Augustine Alanga told the BBC's Newsday programme
He said it was "pathetic" that the university was only guarded by two police officers.
Image captionAll students have been accounted for, said the authorities.Nine critically injured students were airlifted to the capital Nairobi for treatment, disaster management officials said.
But each student had been accounted for by the end of the evacuation.
An overnight curfew has been implemented in Garissa and three other counties in Kenya.
How attack unfolded:
1. Militants enter the university grounds, two guards are shot dead
2. Shooting begins within the campus
3. Students attacked in their classrooms while preparing for exams
4. Gunmen believed isolated in the female dormitories
5. Some students make an escape through the fence
Who are al-Shabab?
UN Secretary-General Ban Ki-moon condemned what he called a "terrorist attack" and said the UN was ready to help Kenya "prevent and counter terrorism and violent extremism".
The United States said it was offering Nairobi assistance to take on al-Shabab and would continue to work with others in the region to take on the group.
The Kenyan government has named Mohamed Kuno, a high-ranking al-Shabab official, as the mastermind of the attack.
Media captionPresident Uhuru Kenyatta: "This is a moment for everyone... to be vigilant"Mr Kuno was headmaster at an Islamic school in Garissa before he quit in 2007.
Kenyan President Uhuru Kenyatta offered his condolences to families of the victims and ordered "urgent steps" to ensure police recruits could begin training immediately. "We have suffered unnecessarily due to shortage of security personnel," he said.
Image captionThe government has placed a $217,000 bounty on Mohamed KunoAl-Shabab says it attacked the university because it is at war with Kenya, BBC Africa analyst Mary Harper reports.
Kenyan troops entered Somalia in October 2011 in an effort to stop the Islamists from crossing the long, porous border between the two countries and kidnapping people - but their presence achieved the opposite effect, provoking al-Shabab to increase its activity in Kenya, our correspondent adds.
The group was behind the Westgate shopping mall attack, when 67 people were killed.