BENGHAZI, Libya — Gunshots didn’t distract Nizar Al-Magrebi from the book he was reading in a cafe. The teenager was determined to better himself.

“The war made it clear to me how important it is to study hard to have a better life,” said Mr. Al-Magrebi, 18, who dreams of becoming an aeronautical engineer. “Cousins of mine died fighting the terrorists. I cannot give up. I should build a peaceful future in their tribute.”