In its ruling on Tuesday the criminal court of Basel-City took into consideration a psychiatric report that concluded the man suffered from schizophrenia, the ATS news agency reported.
The court ordered the man to receive psychiatric treatment at an institution for a five-year period, ATS said.
This period can be extended by a judge if the conditions for his release are not met.
The killings date back to the beginning of November 2014 when the convicted man in a crazed state used a knife to slash a woman to death in an apartment in the Breitequartiers neighbourhood of Basel.
He then stabbed another woman to death before attacking a man, who suffered serious knife wounds.
Riot police later surrounded the man's apartment before arresting him.
“People who are not responsible for their acts can also be murderers,” the chief judge of the court is quoted by ATS as saying.
The convicted man has been receiving treatment at a secure psychiatric clinic since May.