As far as I know, margarine is the only word in which a 'g' is pronounced as 'j' though it is not followed by 'e', 'i', or 'y'. What causes the unorthodox pronunciation?
The OED says:
N.E.D. (1905 ) gives as the pronunciation only (mā·ɹgărīn), with /-g-/ ; this pronunciation, which became rare in the second half of the 20th cent., probably underlies the nickname Maggie Ann (see maggie n. 4). N.E.D. (1902 ), however, s.v. Oleomargarine, notes that the latter is ‘Often mispronounced (-mā·ɹdʒərīn), as if spelt -margerine’ (i.e. with /-dʒ-/ ). The latter pronunciation is recorded in 1913 (with subordinate status) by H. Michaelis & D. Jones Phonetic Dict. Eng. Lang.; the shortened form marge, in which -ge also implies pronunciation with /-dʒ-/ , is attested within ten years of this (see 1922 at marge n.2). The shift of stress, outside North American English, from the first to the final syllable is also first evidenced in the 1913 source.
I doubt if we will find any more definite answer than this.