The 26-year-old told an appeal court in Sundsvall in northern Sweden that he did not have "the intention" to have sex and that he suffered from "sexsomnia".
His argument was upheld by the court, which overturned a previous previous two-year-prison sentence.
The man's defence that he "was in a state of sleepiness, unconscious of what was happening, does not seem absurd", the court said in its judgement, issued on September 8 and obtained by media in Sweden on Thursday.
The decision was mainly motivated by the intervention of a doctor specialising in sleep disorders who said that the defendant could suffer from sexsomnia, a state in which a person can have sex asleep.
The theory was confirmed by the man's previous partner.
According to psychiatrists specialising in sexsomnia, a condition that has not been widely researched yet, it is a sleeping disorder close to
sleepwalking which includes sexual behaviour.
Those affected by sexsomnia are completely unaware of their acts, specialists say.
However, the affliction is very controversial among physicians and lawyers.