This is called the "Thrifty Gene Hypothesis" which was first used to explain why diabetes is so common. Basically it suggests that these alleles would have provided some kind of advantage, over the other possible alleles at that loci, until the environment changed. Then the environment changed and the allele became harmful. Environments are always changing and therefore favour different alleles (variant of a loci).
I suggest the genes linked to obesity in the modern world would, if tested on the old world populations would have been linked to being of a healthy weight. Further, those without it may have been skinnier and more likely to suffer from the disadvantages of being in such condition.
However, this paper present some counter arguments to the thrifty gene hypothesis. Speakman suggests that the genes responsible spread via genetic (allelic) drift rather than by positive selection. It is worth remembering that drift is very poor at spreading new mutations through large populations so there are also weaknesses to his arguments.
One example of a change in the favoured allele caused by an environmental shift is the DDT resistance gene in Drosophila which was present in the population at very low levels for a long period of time. As an aside, it possibly remained this way because it was subject to sexually antagonistic selection - beneficial to females whilst detrimental to males. It then spread through the population when DDT pesticides were used because it obviously became highly beneficial. Many other examples of environmental change leading to different selection are around, this is just one off the top of my head. Another is the Galapagos Finches in a drought in the late 20th century.
HTH, if not please ask more and if it does then please accept it!