Blood stained the steps to the university and staff and paramedics carried out bodies in bags.
The gunman, a Georgian citizen, was also among the dead. Officials could not say how he had died, but local media said he had killed himself.
He killed a security guard and a cleaner as he entered the building, before opening fire on students and teachers, Azeri ANS television reported.
Two people from Syria and Sudan were among the dead, a police source said.
"We all share your grief in these hard days," Ilham Aliyev, the country's president, said in a message to the families of the victims. "The government ... will undertake all measures regarding this incident."
Police found three ammunition belts and 71 bullets on the body of the gunman, reported to be 29 years old.
A senior aide to Aliyev said the motive did not appear to be political, Azeri Trend news agency reported. "Such incidents happen in many countries," Ali Hasanov said
Bekir Belek, a Turkish student, said from a hospital in Baku: "We were in an exam, we heard gunshots, we went out of the classroom in panic and saw a gunman opening fire on everyone, three of my friends were shot."
"Everywhere was covered in blood, all corridors. There are many wounded," Belek said.
"We were trying to escape but had to return when my friends were shot, we took them to hospital."
Al Jazeera's Matthew Collin, in Tblisi, the capital of neighbouring Georgia, said the motive for the shootings was unknown.
Oil and gas-producing Azerbaijan, a largely Muslim ex-Soviet Republic that borders Russia, is officially at war with neighbouring Armenia but there has been no recent incidents of violence over relations between the two nations.