A baby boy born 16 weeks prematurely was declared dead by doctors at a hospital in Paraguay only to wake up in time for his funeral wake hours later.

Dr. Ernesto Weber, head of pediatric care at the state-run hospital in the capital of Asuncion, said the baby weighed just 500 grams (17.6 ounces) when he was born.

“Initially, the baby didn’t move, he practically didn’t have any respiratory reflexes, nor did we hear a heartbeat and, as a result, we declared a premature fetus of 24 weeks dead,” Weber told Reuters Television.

The family was given a death certificate and a cardboard box with the baby’s name scribbled on the outside which served as a makeshift coffin.

But when the family took him from the hospital to prepare him for his funeral, the unbelievable happened.

“I opened the box and took the baby out and he cried. I got scared and I said “the baby’s crying” … and then he started moving his arms, his legs and I got scared, we got very scared,” said one member of the family, Liliana Alvarenga.

Hours after the baby’s death had been declared he was found to be alive. The hospital has begun an investigation and the baby is now in a stable condition in an incubator.