Understanding how different ecological types within a species are formed and sustained is a key question in evolutionary biology. We examine this question through comprehensive resequencing of the genomes in a species of toad-headed agama (Phrynocephalus putjatai) endemic to the northeastern Qinghai-Tibetan Plateau, which exhibits considerable adaptive variation across three distinct habitats: desert, semi-desert, and meadow. We revealed the differentiation of these ecotypes started ~10,000 years ago and identified varying degrees of gene flow among them. The desert and meadow ecotypes originated from the semi-desert ecotype, and subsequently underwent directional selection in two contrasting habitats of desert and meadow. By analyzing the genomic basis of ecotype differentiation, we identified a single ~170-kb locus encompassing two tightly linked MYH genes, which may play a major role in facilitating the rapid adaptation to desert and meadow habitat in P. putjatai. This locus is likely to have experienced recent strong positive selection in both desert and meadow ecotypes and originated prior to their divergence. Our findings provide compelling evidence that the P. putjatai ecotypes recently diverged from a common source, and local adaptation to different habitats involves the sorting of ancestral genetic variation. This study underscores how selection acting on ancestral standing genetic variation can drive rapid local adaptation and diversification despite gene flow.