Back and knee pain while sitting down during meditation is pretty common.
Even amongst people who are physically active, sitting down for long periods of time in a meditation pose can be quite difficult and painful.
Usually this is just a sign that your muscles are not used to the sitting position you are in, especially when sitting for longer periods of time.
People are normally not used to sitting up straight like you would during meditation, so even if someone who is generally physically active sits and meditates, they could also experience some discomfort in their back and knees due to those specific muscles not being used to being in that position.
For the knee and back pain, I find that the more consistent the practice is, the less they hurt. This is because the muscles get used to the sitting position. Though, this does not mean that you should sit for hours, and eventually it will stop hurting.
A consistent practice can also mean a 5-minute session once a day.
With all of that said and while slight discomfort when starting out is quite normal, if it really hurts to sit and meditate to the extent where you cannot continue, and you are suffering, then it may be worth checking it out with a doctor or a specialist, just to make sure nothing is wrong.
As for if improving flexibility can help, from my personal experience, yes, it can. I have also experienced pain in my upper and lower back, and in my knees while meditating. Usually it would start after around 5 minutes. Slowly, after consistently meditating for 10 minutes a day, the pain started to go away, and instead of it starting after 5 minutes, it would start after around 10 minutes, it would just get better and better slowly.
After stretching though, even while sitting for a 15-20 minute session, I wouldn't feel the pain. This isn't just pre-meditation stretching though, I would stretch on a regular basis.