In general, it is not recommended, but don’t worry about it if it happens occasionally!
Most pregnant women have heard that sleeping on your back is not recommended after the first trimester of pregnancy. This is because as the uterus grows, laying flat can cause it to compress on the major blood vessels in the body that bring blood to and from the lower half of your body. This can lead to you feeling dizzy from low blood pressure and may also worsen problems like swelling in your legs and hemorrhoids. However, occasionally waking up flat on your back is not a major concern. As you sleep at night, you are constantly shifting positions. For example, if you have been on your back a while and your blood pressure started to drop, you would unconsciously adjust to sleeping in a better position. So having woken up on your back might mean you only recently landed in that position. If you are worried, however, you can always use a body pillow to help keep you comfortable.