It can occur for many reasons. One can get it from standing with legs locked too long, heat, vasovagal reactions from pregnancy, nausea, emotion or a myriad of other reasons. More common in young people with more reactive circulatory systems. When you get older the pipes get a little stiffer and you are not as prone to it
We even had this occur in a Young Marines troop (ages mostly 8-16), while standing in formation especially on a hot day. Had to be counseled each time to not stand with the knees locked.