Why is "er" sometimes pronounced with a long a sound as in the words inherit, ceremonial, and hysteria? (But not in the words exercise or commercial)
the "air" pronunciation is possible (for some speakers) for "er" before a vowel letter or another letter "r"
In general, the sequence of letters "er" may correspond to the same sound as "air" for speakers with the merry-Mary merger when it comes before a vowel letter or before another letter "r". This is just the result of the merger of the "short e" and "long a" sounds before "r" that occurs for many North American speakers. Beneath that, it's a straightforward case of the letter "e" representing a "short e" sound, which is actually one of the less strange correspondences of English spelling. (By the way, "hysteria" has another pronunciation that uses the "long e" (or for speakers with the Sirius-serious merger, "short i") vowel of "mysterious" instead.)
the "air" pronunciation is usually impossible for "er" before another consonant letter
The words "exercise" and "commercial" aren't pronounced with the "short e" vowel because they have the letters "er" in quite a different environment: before another consonant letter ("c" in both cases). This is just like how arrow and art don't start with the same vowel sound (or another example: mirror vs. mirth). It's really common in English for vowel letters to represent different sounds before "r" followed by a consonant (or a blank space) and before "r" followed by a vowel letter or another letter "r".
There are, of course, exceptions, like "squirrel," which for most (maybe all?) North American speakers is prononced like "squirl" or "squir-l" rather than as "squeerel".
With "er", the exceptions I know of are the verb "err" (which was traditionally pronounced like "her" without the h, but is now often pronounced as "air" by North American speakers), and the word "ergo," which often is pronounced like "airgo" because people feel like they need to make the vowel closer to what it was in Latin.
Also, words like "preferring", "transferring" and so on don't have the same vowel as "cherry" for the same reason that "starring" and "starry" don't have the same vowel as "carry": the r-doubling is only the result of suffixation, and in the base word the "r" is word-final.