The male hemipenes may be pushed out during defecation, or in conjunction with depositing some seminal material - and the hemipenes should revert back (be pulled back in by the muscles) into their normal position within a few moments.
A prolapse where it stays out can be due to constipation or impaction, and I believe certain parasite infestations.
Mitigate chances of this situation by keeping chameleon well hydrated, dont feed too many hard-to-digest prey items, avoid substrate.