Why does "contrary" have two different pronunciations?

Solution 1:

Apparently the original stress was on the second syllable, but poets bent its stress to whatever suited them. For example, both Chaucer and Shakespeare were known to use both versions (conˈtrary and ˈcontrary), although Shakespeare seems to have more often used the one with initial stress.

You can get all that from the OED’s elaborative note regarding the stress of contrary:

The later OFr. form contraire gave the variant contrair, long retained in the north. The original stress, after Fr. and L., wasconˈtra.rie, but the poets, from Chaucer to Spenser and Shakspere, use both conˈtra.ry and ˈco.ntrary (the latter the more frequent in Shaks.); of conˈtrā.ry, many instances occur in 17th c. verse; it is the only pronunciation recognized by Bailey (died 1742), and it is still app. universal in dialect and uneducated speech, esp. in sense (def#3)(def#b), which is now confined to these forms of speech and to the nursery. ˈCo.ntrary was used by Milton and Pope, and is given by Johnson (though he retained conˈtra.rily, conˈtra.riness, conˈtra.riwise) and in all later dictionaries.

Solution 2:

Mary Mary quite cont-RARE-ee is an adjectival use.

On the CONT-trary, this is a noun.