This happens due to mal-alignment of the knee
cap on bending
(and hence any imaging study has to be taken with the knee
bent), injury to the knee (you may have sustained it in past), flat feet, over tight or over lax muscles around the knee joint. All these aspects will have to be investigated by your doctor. Other possibilities are bursitis (inflammation of fluid filled sac over joints), tendonitis (inflammation of cords joining muscles to bones) or due to weakness of patellar bone.