When does Magic Lamp let me keep the treasure?

If you are fighting one monster, and use Magic Lamp on that single monster, you collect the treasures from that monster. If there are multiple monsters, and you use Magic Lamp to get rid of one of the monsters, you do not get any of the treasures from that monster even if you defeat the remaining ones (you would still get the treasure from those monsters, though).