When Russia invaded Ukraine, Mohamed Elfatih Ahmed was 18 months from achieving his lifelong dream of becoming a doctor.

Even when missiles started raining down on the Ukrainian port of Odesa, where Mr. Ahmed had come from Sudan to study medicine, he was determined to stay.

“I was so close to graduating,” said Mr. Ahmed, who reached Odesa through years of family savings and a scholarship, and is the only one of 10 siblings to attend university. “No matter what, I knew I could not go back home empty-handed.”

He is among 26,500 Africans who were studying in Ukraine before the full-scale Russian invasion in February of last year, according to Ukrainian government records. Thousands of students fled in the early months of the war, among them Mr. Ahmed, but unlike the millions of Ukrainian refugees alongside them, many of the students have largely had to fend for themselves.

European countries offered limited support, if any at all, leaving the students to navigate thickets of rules about visas, university credits and possible deportation. For many, going home could be unsafe, and for most it means years of lost hard work and tuition fees, their dreams indefinitely deferred.

Ukraine used to be a popular destination for foreign students, mainly for medicine and engineering, because it offered lower fees and looser visa regulations than many European countries or the United States, as well as the promise of a diploma that could lead to a European career.

But when the war broke out, many African students struggled to get away, with some saying they were pushed to the end of the line at overwhelmed border crossings and forced out of trains and buses. The Ukrainian authorities have denied discriminating.

A year on, many of those students are stuck in limbo, unable to continue their studies either in Europe or in their home countries, which often do not recognize Ukrainian university credits. Determined to finish their degrees, some have decided to return to Ukraine despite the risks.

Those who fled to European Union countries are struggling to manage a complex legal landscape, and to gather enough money to get by.

In the early days of the war, conflicting rumors were whizzing around and access to official information was scarce, said Mr. Ahmed, 23, making it hard to decide what to do. After several sleepless nights under the roar of falling bombs, he decided to flee, first to Poland, then to Germany.

“The advice was to go to the border,” he said.

But for many African students like him, the path has not been as straightforward as that.

Four million people fled Ukraine to seek safety in the European Union. The bloc has granted Ukrainians, as well as foreigners who were long-term residents or refugees there, the right to live, study and work in E.U. nations for three years. But foreign students were not included, and their legal status was left up to each European country.

Over the course of last year, the bloc absorbed the highest number of newcomers since World War II, significantly straining its already overstretched asylum and housing systems.

Some countries, like Belgium or Poland, did not set up any special rules for African students, expecting them to leave or apply for asylum. Others, like Germany or the Netherlands, created programs that allowed them to stay for a limited amount of time, with financial support.

These initiatives are now expiring, plunging students into legal gray zones — and deeper into despair.

Osinachi Ekenulo, a 25-year old from Nigeria, spent a year living with host families in Amsterdam after fleeing Ternopil, in western Ukraine, where she was in her final year of her medical degree. She applied to several schools across Europe, without success.

Going back to Nigeria is “not an option,” Ms. Ekenulo said, citing security concerns, a dire economic situation and regular university strikes. And if she went back, her credits from Ukraine would not be recognized, she said, erasing four years’ work.

“I would have to start from scratch,” she said. “If I stop now, I’ll have nothing to show for all these years.”

The Dutch authorities told Ms. Ekenulo that she had until March to leave the country, and at the 11th hour, a medical school in Georgia accepted her. She is now attending classes in Tbilisi, the capital, but her ordeal is not over: Her student visa application was just rejected, and her lawyer has appealed.

It is unclear how many African students fled Ukraine for elsewhere in Europe. The Dutch government believes some 6,000 temporarily settled there. In Hamburg, Germany, where Mr. Ahmed lives, there are at least 800, the local authorities said.

The uncertainty, compounded by the experience of what they went through in Ukraine, has taken a heavy toll on many students, experts said.

Abigail Oni, a trauma therapist based in Abuja, Nigeria, has been providing free sessions to some. Many are suffering from anxiety, depression and post-traumatic stress disorder, she said. They reported trouble eating and sleeping. Some told her they were contemplating suicide.

Many dreaded the prospect of going back home.

“Whole families take loans to finance their studies, there is so much pressure,” Ms. Oni said. “Coming back home with nothing triggers feelings of shame and guilt.”

Unlike other E.U. countries, Portugal extended the same rights granted to Ukrainian refugees to African students who had fled, providing housing and financial support and, for a limited time, allowing them to apply for an emergency university program.

Ahmed Habboubi, a 23-year-old Tunisian who was studying medicine in Odesa, is one of them. Mr. Habboubi initially fled to France, but after failing to secure a residency permit, he moved to Portugal. He is currently in Porto, learning Portuguese and waiting to hear whether a medical school accepted him.

Some 8,000 Africans who fled Ukraine were granted protection in Portugal, according to the authorities, although it is unclear how many were students.

Most universities in Ukraine are now open, and many offer remote learning. But medical students say it is not an option for them, because some courses still require students to attend in person.

Last month, final-year students received letters from their universities ordering them to take the medical licensing exam in person, or face expulsion. Some schools even asked students to sign a disclaimer saying they were aware of the risks of returning to a war zone, and were responsible for their “own safety and life.” The universities cited government orders.

The exam was held on March 14. Only two weeks later, the authorities announced that students would be allowed to take it online. Ukraine’s Education Ministry denied ordering foreign students to return. “There was an order that the exam is restarted and scheduled, but nothing about online or offline possibilities to pass it,” the ministry said.

After his six-month permit in Germany expired in November, Mr. Ahmed considered applying for asylum, but said that officials discouraged him because Germany considers Sudan a safe country.

Then, near the end of the year, he received a deportation order. And while he has appealed the decision, he has been cut off from state support — and is still unable to pursue his degree.

“It just makes you want to stop in the street and scream,” Mr. Ahmed said. “They told us: We need doctors in Europe. We are here, but they don’t want us.”