It could be a lot of things. My kneecap will sometimes dislocate and slip to the side, the first time it happened it hurt so badly I actually passed out. But then I learned how to handle it. When this happens, someone has to do exactly what you said: I have to try as best I can to relax with my leg bent, not flex any muscles in my leg. Then someone has to pick up my bent leg, support all the weight with one hand under my foot and the other under my thigh (the not flexing any muscles to try to help is key here, because that interferes and is incredibly painful), and very slowly straighten my leg and the kneecap will pop back into place.
I went to the dr the first time, and he said it was because I had a muscle imbalance - my outer thigh was stronger than my inner thigh, and that put outward pressure on the ligaments and would cause my kneecap to slide out. The long-term solution is to stay in shape, because if my inner thigh stays strong and my leg muscles are balanced, it won't happen.
You might have a similar issue - but you should talk to a doctor and/or physical therapist to find out.