A woman "sacrificed" her daughter to Allah to "exorcise evil spirits", the Old Bailey has heard.
The girl's body was found at her east London flat on the Chatsworth Estate, Clapton, east London, in December.
Ali pleaded guilty to manslaughter on the grounds of diminished responsibility. She was sent to a mental hospital indefinitely.
When police arrived at their east London home, the woman was chanting: "I seek refuge in God from the curse of Satan".
The Old Bailey heard Ali had sunk "suddenly and deeply into mental illness" and was convinced the spirits or jinn had entered the bodies of members of her family.
Her daughter had remained at the flat in December because she had been ill and was not allowed to go to nursery.
After her arrest, Ali told her husband: "Suddenly it came into my mind that I should correct my behaviour towards Allah.
"Then a voice told me 'if you love Allah you should sacrifice your daughter."'
She told a doctor she strangled the child and when she was unconscious, she carried her on to the kitchen table where she used the knife to ensure the spirits were exorcised.
As she stabbed the girl, Koranic verses were played in the background on an electronic device, the court heard.
Judge Anthony Morris told Ali: "One of the horrifying aspects of this case is how quickly you lost control of yourself."
Gary Dolby, head of London's Crown Prosecution Service Homicide Unit, said: "Family members and police officers encountered appalling scenes when they entered the house.
"This is clearly a very sad case and our thoughts go to the family at this difficult time."