Social services bosses were blamed for "nightmare" litigation after a woman changed her mind about giving a baby up for adoption.
Both sets of parents were then involved in a family court battle over the girl.
High Court Judge Mr Justice Francis blamed a Welsh council for failing to keep the baby with her birth mother.
She was given to a new family at three hours old, but the court ruled shortly before her first birthday she should be returned to her biological parents.
Details of the case have emerged in a written ruling which has been published.
The couple who wanted to adopt had asked Mr Justice Francis to let them make an adoption application.
But the judge ruled against them after concluding that such an application was "bound to fail".
He said the little girl should leave the couple's home and be integrated back into her birth family.
The judge, who analysed evidence at a family court trial in Cardiff, said a concern had been raised about the girl's mother's mental health around the time of the birth.
But he said a social worker had not counselled the woman, before or following the birth, and a Welsh council had not done all it could to ensure that the youngster could be brought up in her birth family if possible.
The judge said as a result "two decent couples" found themselves "pitted against each other" in litigation" they could not have "wished for in their worst nightmares".
He said the "human misery" in the courtroom during the trial had been "palpable".
The judge said the litigation had been "necessitated" by the council's "failings".
Lawyers representing the council had apologised, the judge said.
Mr Justice Francis said no-one involved in the case - including the council - could be identified.