Good question. Yes, X is indeed pronounced /gz/ in certain words, like exam and example.
We're sometimes asked why we don't teach /gz/ as another sound of x for students to memorize. That would certainly be one way to teach it, but we approach words like this a different way, by teaching students about how sounds assimilate and why this happens. This is because what happens in this word is actually an instance of a widespread and common phenomenon in human speech.
All speech sounds can take slightly different forms in different sound "environments" - that is, what sounds are around them in a word - in order to make the words easier to pronounce. Often these changes are so subtle that we don't notice them (for example, most people don't notice that they say a slightly different version of /p/ in "pie" than in "spy"). Some of them are more significant in some speakers' speech, or in some dialects, than others. Others are much more noticeable: /tūr/ assimilates to /cher/ in "nature" and "architecture," /zū/ assimilates to /zhū/ in "unusual," /sūr/ assimilates to /sher/ in "treasure," etc. These instances of assimilation happen in almost everyone's speech. Many of them are quite common: for example, in American English, at least, most speakers pronounce the /ă/ in "man" (as well as in "exam" and "example") quite differently than they do the /ă/ in cat or clap.
So we find it helpful to teach students about this concept of assimilation and help them feel for themselves what it is and why it happens.
We approach the sound X makes in "exam" the same way: We'd talk directly with students about how it "should," according to the normal sound of phonogram, be pronounced /eksam/, help them notice the difference between that and how it's actually pronounced, and have them feel where in their mouths the sounds /ĕ/, /k/, /s/, and /g/ are formed. They can try saying /eksam/ and /egsam/ and feel how the /g/ is closer to the /ĕ/ and the /s/ and easier to say between them. Once students can feel this themselves, they usually find it pretty easy to understand how people's mouths just took the "easy road" and shifted this sound a bit to a similar sound that is a little easier to say.