The surgery lasted nearly five hours and the 2-year-old boy was in stable condition after the procedure, said Susy Moreno, a spokeswoman for the hospital in the northeastern city of Salvador where the boy was in intensive care.
"He's OK, the surgery was a success, he's doing fine," she said in a telephone interview.
Dozens more needles measuring up to 2 inches (5 centimeters) in length remain are still inside the boy's body, but the four removed were considered the most threatening to his life.
Doctors will evaluate the boy's recovery from surgery before deciding when to perform at least two more surgeries to remove more needles, she said.
Police say the boy's stepfather, 30-year-old bricklayer Roberto Carlos Magalhaes, confessed to pushing supposedly "blessed" sewing needles deep into the child because his lover told him to while in trances.
The rituals were performed over a period of a month to try to keep the couple together, the stepfather told police. Authorities suspect the woman was trying to take revenge on the wife of her lover by having the man hurt her son.
Magalhaes told detectives the woman would enter into trances and give him commands to insert the needles, police inspector Helder Fernandes Santana said. The stepfather told police the rituals happened every few days for a full month, with him inserting several needles during each session.
The lover, Angelina Ribeiro dos Santos, paid to have the needles blessed by a woman who practiced the Afro-Brazilian religion Candomble, and convinced Magalhaes that inserting them into the boy would somehow allow them to be together, Santana said.
Authorities initially estimated the boy had as many as 50 needles were inside the boy. After batteries of tests were performed, doctors now believe there are closer to 30 needles inside, but they don't know for sure.
"They haven't focused on how many there are because they are concentrating on the most dangerous ones," Moreno said.
The boy was also suffering from an infection from one needle, but received antibiotics and was in stable condition and breathing on his own before going into surgery, she said.
Magalhaes and dos Santos were both arrested, though no charges were filed.
Dos Santos is not believed to be a member of any religious or occult group, and authorities believe she came up with the idea of the rituals on her own, Santana said.
The two were taken to an undisclosed lockup for their own protection after a mob threw stones at the police station where they were being held. It was not immediately clear whether they had legal representation.
Authorities also detained the woman who blessed the needles so she could be questioned, but Santana has said he expects she will be released without charge because she did not know how they were being used.
The boy's mother, a maid, took him to her hometown hospital in Ibotirama on Dec. 10, saying he was complaining of pain.
After X-rays revealed the cause, the mother told police she didn't know how the needles got inside her son, whose name was not released because of his age. The boy was later transferred to the much larger hospital in the coastal city of Salvador.
Police and doctors concluded it would have been impossible for the boy to have ingested the needles, which have been found throughout his abdomen, in one leg and in his spine.
Afro-Brazilian religions practiced in Brazil have no ceremonies, rituals or practices involving harm to people, said Nelson Inocencio, director of African-Brazilian studies at the University of Brasilia.
He worried that the incident could hurt the image of the religions, of which Candomble is the most popular, and concentrated most in Bahia state where Ibotirama is located.
Associated Press Writer Marco Sibaja contributed from Brasilia, Brazil.