Tayyab Shabab, who works as an app developer for the company Dynamo, had won the backing of Stockholm tech leaders such as Spotify founder Daniel Ek.
But on Tuesday, Stockholm's Migration Court upheld the Swedish Migration Agency's decision to refuse to extend his work permit because of an error made by a previous employer.
Shabab told Radio Sweden that the decision had been an unexpected blow.
"It was so terrible. I wasn't expecting it at all," he said. "It was devastating."