To answer your question, you must understand the Hebrew genitive construction and the construct state of a noun.
According to Wikipedia on the word סְמִיכוּת,
סמיכות היא צירוף של שני שמות עצם שיש ביניהם קשר הדוק. השם הראשון בצירוף נקרא נִסְמָךְ והשני נקרא סוֹמֵךְ.
A smikhut (סְמִיכוּת) is a combination of two nouns which have a close connection between them. The first noun in the combination is called nismakh (נִסְמָךְ), and the second [noun] is called somekh (סוֹמֵךְ).
Each of the phrases in your question is a smikhut, or “genitive construction”:
In a smikhut, the spelling of אֱלֹהִים as the נִסְמָךְ (head/first noun) will change to אֱלֹהֵי.
The spelling of the סוֹמֵךְ or second noun in the smikhut remains the same.
In Basics of Biblical Hebrew Grammar, Pratico and Van Pelt write,1
To answer the question you presented in the comments:
I gather that elohei is a plural construct noun "Gods of" However, I cannot make sense out of how it was in used in its context like "Gods of Gods"? How you understand it?
In Hebrew, the grammatically plural word אֱלֹהִים can be translated into English as the singular “god” or the plural “gods.” This would also apply to its construct form אֱלֹהֵי, except that you would add “of” after it (i.e., “[the] god of” or “[the] gods of”). The context typically assists the translator in determining which. Now, suppose you were inclined to translate it as the plural “gods of” rather than the singular “god of.” If you read further in the verse, you would encounter the phrase הָאֵל הַגָּדֹל הַגִּבֹּר וְהַנּוֹרָא (ha-el ha-hadol ha-gibbor veha-nora). Since these words are declined in the singular number, and they, like אֱלֹהֵי הָאֱלֹהִים וַאֲדֹנֵי הָאֲדֹנִים, all refer to the same subject that occurs earlier in the verse, i.e., יהוה, you could be assured that אֱלֹהֵי should be translated as the singular “the god of.” Also, there’s the pronoun הוּא (hu) which precedes the phrase in question; that pronoun is declined in the 3rd person, singular number: “he.”
1 p. 101
Pratico, Gary D.; Van Pelt, Miles V. Basics of Biblical Hebrew Grammar: Second Edition. 2nd ed. Grand Rapids: Zondervan, 2007.