Modern usage represents a seventeenth-century merger of the glyph ㅇ, which usually represented a null consonant but sometimes the fricative /ɣ~ɦ/ in Middle Korean, and the now obsolete glyph ㆁ (ng), which represented /ŋ/. Because a null coda consonant is not written in Korean while /ŋ/ appears only at a syllable coda, no ambiguity was created by this merger.
The Hunmin Jeongeum Haerye, the treatise introducing the principles behind the Korean alphabet written by its inventor King Sejong in 1446, explains that ㅇ was derived from the "outline of the throat", as is visible when pronouncing a vowel or a laryngeal consonant. Gari Ledyard concurs, as there is no possible 'Phags-pa source.
Sejong explains that the form of ㆁ (ng) was chosen despite the fact that /ŋ/ is a velar—and thus should theoretically be visually related to the other velar consonants, ㄱ (g) and ㅋ (k)—because "the pronunciation resembles that of ㅇ [/∅~ɣ~ɦ/]", noting that the dialects of some Chinese rime dictionaries have in fact lost initial /ŋ/. (The Korean pronunciation of Chinese also dropped initial /ŋ/, although he does not explicitly give this as a reason.) Ledyard suggests that Sejong derived the nasal letters by removing strokes from the glyphs for the plosives. But the glyph for /k/ was ㄱ, from which removing a stroke would provoke confusion with the vowel glyphs ㅣ (i) or ㅡ (eu). According to Ledyard, Sejong thus chose ㅇ as the visual basis of the letter for /ŋ/, both to avoid confusion with vowels and to note the fact that both Sino-Korean and many Chinese varieties dropped initial /ŋ/, while the added stroke on top of ㆁ was regularly derived from ㄱ (g) to mark the velar nature of the consonant.