I had that similar scenario for months, a few years back. I would wake up with legs bent and back curled, as that’s how I sleep, and it would take quite a bit of time and stretching to be able to do stuff.
Finally went to doc, then an MRI as I had private healthcare through work, which showed up a little ‘lump’ for want of a better word on one of my discs, which would press against the spinal column and for some reason sleep exacerbated it.
Told me that I didn’t require an op, it would eventually alleviate itself, could be a month, could be a year. True enough, a couple of months after it slowly stopped, and I’ve never had any such issues since.
Just my experience, HTH.