Well, a solid ten minutes of googling has told me the idea comes from arabian nights, which is perhaps unsurprising. The story of aladdin was in fact the first tale to include a lamp, but there were others that included genies (or jinnis, apparently what theyre called there) in brass objects and stuff.