The singer says, "It was the saddest thing I've ever been through."
"About two years ago, I was pregnant for the first time. And I heard the heartbeat, which was the most beautiful music I ever heard in my life," she said. "I picked out names. I envisioned what my child would look like ... I was feeling very maternal."
Unfortunately, a trip to the doctor for a checkup revealed that the baby's heart had stopped beating. So she retreated into her music to help her cope with the tragic loss.
"I went into the studio and wrote the saddest song I've ever written in my life," she says. "And it was actually the first song I wrote for my album. And it was the best form of therapy for me, because it was the saddest thing I've ever been through."
She later became pregnant again and gave birth to daughter Blue Ivy in January 2012.
Beyoncé is expected to reveal more personal details of her life in the documentary, which airs Feb. 16 on HBO.