Associated Press photojournalist Massoud Hossaini, who was among the trapped, was said to be wounded and later managed to escape with some fellow students.
"Many students have been evacuated," said interior ministry spokesman Sediq Sediqqi, adding that there were no immediate reports of any hostages taken by the attackers. "We are not sure about the number of attackers but our special forces have started clearance operations."
US military advisers were assisting Afghan forces to respond to the attack, an American defense official said.
Kabul-based journalist Ahmad Mukhtar, a university student, tweeted that he managed to escape but "several of my friends and professors (are) trapped inside".
Ambulances rushed to the scene as erratic gunshots rang out from inside the campus.
The Italian-run Emergency Hospital in Kabul tweeted that at least five wounded people had been brought to the facility for treatment.