It comes from the "x." X has a variety of possible pronunciations, as shown by this teflpedia article. While the most common pronunciation of "x" is as /ks/, it is also not rare for it to be pronounced /gz/. These pronunciations are related: the consonants /k/ and /g/ are pronounced at the same position in the mouth (they are both velar consonants) and share the same manner of articulation (they are both plosives). The difference is that the first is classified as phonologically "voiceless" and the second is classified as phonologically "voiced."
The voiced pronunciation of "x" is more common before a stressed syllable, although there are exceptions to this tendency in both directions (exit may be pronounced with [gz] even though the "x" comes after the stressed syllable, and maxilla is pronounced with [ks] even though the "x" comes before the stressed syllable).
The velar nasal
Another velar consonant is the velar nasal [ŋ], as in the word sing. As the spelling suggests, in English this sound is related to both the alveolar nasal /n/ and the voiced velar plosive /g/. Historically in English, /n/ was pronounced as [ŋ] before a velar plosive like /g/ or /k/. This is called assimilation: the sound changes to be pronounced in the same mouth position as an adjacent consonant.
An example of the sequence [ŋk] is the word sink, which is actually pronounced like "sing" followed by "k." We don't write "singk"; in this context, [ŋ] is just represented by the letter "n." This same assimilation occurs before the letter "x": the word lynx is pronounced [lɪŋks], which sounds like "ling" followed by "ks." Just like we use the spelling "nk" instead of "ngk," we use the spelling "nx" rather than "ngx."
Simplifying consonant clusters that are not allowed in English
All languages have restrictions on permissible consonant clusters. Certain sounds may be dropped or inserted to make sure a word complies with these restrictions.
Standard English syllables cannot start with [ks] or [gz]. So words that start with x, such as xenon, have to take a simplified pronunciation, which turns out to be [z].
In standard English, it's also not possible for a syllable to end in [ŋg], any more than it can end in [mb]. Both of these clusters existed historically (in words like sing and comb), but they were simplified by dropping the final voiced plosive, leaving just a nasal consonant ([ŋ] in sing, and [m] in comb). This is how the digraph "ng" in the spelling of some words came to represent the sound [ŋ].
In the word anxiety, the x takes on a voiced pronunciation, likely because of the stress on the second syllable (compare with the related word anxious where the stress is on the first syllable and the voiceless pronunciation with [k] is used).
The "n" is assimilated to [ŋ], just like in other words spelled with "nx."
However, there is a problem with using the voiced pronunciation [gz] in this position. It would violate the restrictions on English syllable structure that were mentioned above. Depending on how we syllabify the word, we either get a syllable ending in [ŋg], [æŋg'zaɪ.ɪti], which is not allowed, or we get a syllable starting with [gz], [æŋ'gzaɪ.ɪti], which is also not allowed.
So in the standard pronunciation, the voiced plosive [g] is actually absent (like in words that end with ng or with words that start with x) and we just have [ŋz], [æŋ'zaɪ.ɪti].
So there are several explanations for the pronunciation of this word:
- The letter "x" can represent a voiced consonant cluster [gz] in some words, such as exam and auxillary, or a voiced consonant [z] in some other words, such as xenon. The voiced pronunciations are less common than the unvoiced pronunciation [ks], and it happens that there are no other words spelled with nx that have a voiced pronunciation of x aside from anxiety. But it still can be explained the same way.
- In standard English, there isn't strictly speaking a [g] sound in "anxiety." There is a [ŋ] sound, and before the letters "c," "k" or "x," [ŋ] is normally represented with the letter "n," not with the digraph "ng."