"You have violated the law of God," the militiamen told her, Almagrabi said.
"I said, I teach male students, so should I bring a male guardian with me to classroom?" she told The Associated Press.
Not that the university is immune to increasingly bold conservatives' views on the role of women. Almagrabi said one student recently told her she shouldn't be giving lectures because a woman's voice is "awra"—too intimate and shameful to be exposed in public.
The incident in February, which ended with the militiamen allowing Almagrabi to drive home, underlined the bitter irony for women in post-revolution Libya. Women played a major role in the 8-month civil war against dictator Moammar Gadhafi, massing for protests against his regime, selling jewelry to fund rebels, smuggling weapons across enemy lines to rebels.
But since Gadhafi's fall more than 18 months ago, women have been rewarded by seeing their rights hemmed in and restricted.
Women fear worse may yet to come. The country is soon to begin work drafting a new constitution, which activists fear will enshrine the relegation of women to second-class status, given the influence of hard-line Islamists.
"What we aim for right now is not to lose what we had," said Hanan al-Noussori, a lawyer in Libya's second biggest city, Benghazi. "I don't know which path we are heading in. But this is a matter of life or death for us."
Women can cite any number of worrying signs.
Libya's lawlessness is in part to blame. Islamist militiamen have grown more aggressive in unilaterally imposing their own rules on women. Militias, which were initially formed from rebel brigades that battled Gadhafi's troops, hold sway in many cities. They operate with impunity because, with national police and the army in a shambles, the state relies on them as parallel security forces. The state funds a security body made up of militias, trying to keep them loyal, but that has only made them larger and bolder.
More generally, the deeply conservative nature of much of Libyan society is being expressed more freely, often impinging on women. Powerful clerics speak out against the mixing of the sexes and Libya's political leaders themselves have set the tone for a more conservative stance on women.
Almagrabi says the opening salvo came right after Gadhafi's fall in late 2011, in one of the first addresses by then-head of state Mustafa Abdul-Jalil. He declared invalid all laws not conforming to Shariah and specifically vowed to end limits on polygamy. Islamic law allows men to take up to four wives, if they are treated equally, but under Gadhafi men had to get court permission and often permission from their first wife to do so.
"I felt like we were taken like spoils of war," Almagrabi said. "This nation rose up for the sake of the supremacy of the law and now there is a plan to push women back into their homes."
In February, the Supreme Constitutional Court consecrated Abdul-Jalil's announcement, formally ending any conditions on polygamy.
In 2012, at a televised ceremony celebrating the transfer of power to a newly elected parliament, Abdul-Jalil ordered a young presenter, Sarah al-Massalati, to leave the hall because she was not wearing a headscarf.
"We believe, respect and emphasize personal freedoms, but we are also a Muslim nation," Abdul-Jalil said at the time, to cheers from the audience. "I hope everyone understands these words."
Al-Massalati broke into tears. "I felt I was slaughtered," she later told Libyan media.
More recently, militiamen stormed a conference on women's rights and the constitution, held by Magdalene Ubaida and other women rights activists in Benghazi. The gunmen detained Ubaida and two of her colleagues. When they were released and heading to the airport to return to Tripoli, they were seized by more militiamen and beaten.
The incident came after one of the top security officials in Benghazi, Wanis el-Sharif, accused Ubaida of "spoling women" and criticizing Libya's top Muslim official, the grand mufti. The 25-year-old Ubaida, a co-founder of a rights organization called My Right, has since fled to Britain, saying she fears for her life.
The mufti, Sheik Sadeq al-Gharyani, took a hardline on women in a speech he delivered a year ago to a conference titled "the role of Muslim women in reconstruction."
"The state must put an end to the mingling of the sexes in the university, to close this door, this big door for corruption," he said. He urged school and university directors to start separating men and women without waiting for the state to order it.
He also cited a warning by the Prophet Muhammad that women who wear revealing clothing or don't cover their hair are "the people of hell."
Al-Gharyani is considered by some a hero of the revolution, since early on in the uprising against Gadhafi he issued a fatwa or religious edict permitting war against his regime.
Under Gadhafi's 42-year rule, women had a mixed bag.
Gadhafi often presented himself as a defender of women rights and at times made a point of defying strict interpretations of Shariah, since Islamists were among his main enemies.
Female lawyers say the country had one of the Arab world's most pro-women personal status laws, covering marriage, divorce and family law. Unlike Egypt and some other Arab nations, there is no "house of obedience" law by which courts can force women who flee their husbands to return. Women have children's custody rights after divorce.
Libyan society is generally conservative and tribal, and the majority of women wear headscarves. But at the same time, women make up a significant proportion of the work force, run their own businesses and were part of the armed forces. More women are pursuing postgraduate decrees than men.
But women lived under the same repressive regime as men under Gadhafi—and he often implicated women in regime abuses. He created a female security force of "nuns of the revolution," members of which participated in hangings of opposition figures in public or in extractions of confessions aired on TV.
Former aides have told lurid tales of Gadhafi's private life, reporting in books and interviews that he had young women brought forcibly to his Tripoli compound as sex slaves.
Women were prominent in the opposition to Gadhafi. For years, the mothers, sisters and wives of some 1,200 political prisoners massacred by the regime in the Abu Salim prison in 1996 held weekly protests at the security headquarters in Benghazi.
That eventually provided the spark for the revolution in February 2011. When one of the women's lawyers was arrested, they expanded their protest. When regime forces cracked down on them, the entire eastern half of Libya quickly rose up in revolt.
Since Gadhafi's fall, women's rights activists have seen at least one reassuring point. In the first free parliament elections last July, a liberal-leaning coalition came out the victors. The Muslim Brotherhood finished second while a party founded by a prominent jihadi-turned-politician got no seats. Women won 33 of parliament's 200 seats.
Now comes the drafting of the constitution. Rights activists worry that few if any women will be in the 60-member drafting assembly, which will be chosen either by national elections or by parliament.
There is also a consensus among all political parties, liberal and Islamist that the charter will enshrine Shariah as the main source of legislation and forbid any laws contradicting it. The last constitution Libya had—a 1951 charter that Gadhafi annulled—made no mention of Shariah.
Salwa Bugaighis, a rights lawyer, said debate over a Shariah clause is out of the question. "Shariah is a big taboo in Libya."
But she places her hopes on getting other articles into the document explicitly guaranteeing women's rights.
"We are fearful. We are worried and we are watching," she said.