"A neighbour alerted the fire service that they could hear meowing coming from a waste water pipe, thinking that it was a cat that had become trapped," police in Alicante, eastern Spain, said in a statement.
However the firefighters and police discovered instead "the cries of a new born-baby and immediately proceeded to dislodge and save the child…who still had the umbilical cord attached" and was wrapped in plastic bags, the police said.
The baby, a boy weighing 2.1 kilograms (four pounds 10 ounces), was taken to a local hospital in serious condition, "with a fractured forearm and other injuries, but his life is not in danger," the statement said.
It is believed that the child was trapped in the pipe for 40 hours.
After saving the boy, police arrested a 26-year-old woman, who lives in the building, for attempted murder.
She had been admitted to hospital "after saying that she had carried out an abortion at home", police said.
The woman said she did not want the baby, but could not afford an abortion, police added.
Police are also investigating whether a third person may have been involved.