Depends on the story, and IIRC it has shifted with time. But yeah, the general trope is they are stuck in a small vessel, and they offer/are extorted into one or more wishes to free them. Why they are stuck ranges from evil sorcerer trying to enslave them to noble hero stopping their rampage.
Supposedly they are malevolent spirits that sometimes may grant wishes as a reward, not by some compulsion.
Depends on the story, and IIRC it has shifted with time. But yeah, the general trope is they are stuck in a small vessel, and they offer/are extorted into one or more wishes to free them. Why they are stuck ranges from evil sorcerer trying to enslave them to noble hero stopping their rampage.