According to Wiktionary the english word comfort should derive by the french word confort.
So why did english adopted the term replacing the french "n" with the "m"?
In French, “com” and “con” are pronounced the same before a consonant: as /k/ followed by a back rounded nasal vowel. In modern French, it’s usual to use “m” in the spelling of nasal vowels before “p” or “b”, and “n” elsewhere, but in the past spellings with “np” and “nb” were often seen in French.
This probably isn't directly relevant, because as far as I can tell, there doesn’t seem to have been notable variation in French between the specific spellings "nf" and "mf" in this word (or any other word that I know of). However, the variation between the spellings -"np"/"nb" and "mp"/"mb" in older varieties of French may have provided an analogical basis for the change in English of conf- to comf- in the word comfort. Like p and b, the sound f has a labial component (however, f is labiodental while p and b are bilabial).
The pronunciations of /nf/ and /mf/ in English, while not necessarily completely identical, are very similar. The nasal /n/ may be coarticulated with a following consonant sound; in /nf/, this produces a labidental nasal [ɱ], which sounds similar to the bilabial nasal [m]. Furthermore, phonetics sources say that it is also possible for /mf/ to be pronounced as [ɱf]. (But /mf/ has another possible pronunciation that cannot be used for /nf/, as far as I know: something like [mpf], or apparently [ɱp̪f] according to this blog post by John Wells.) This similarity probably contributed to the development of the /mf/ cluster (and "mf" spelling) in comfort.
The use of /m/ in "comfort" doesn't seem to be in accordance with any general rule of English pronunciation; rather, it is exceptional. There are more words that start with "conf-" than there are that start with "comf-". I only know of two other words that start with "comf-": comfit and comfrey (along with derivatives of these three words, of course).
The m probably derives from Latin prefix com-, sometimes used as an intensive, which was adopted from the 14th century:
late 13c., conforten "to cheer up, console, soothe when in grief or trouble," from Old French conforter "to comfort, to solace; to help, strengthen," from Late Latin confortare "to strengthen much" (used in Vulgate), from assimilated form of Latin com-, here probably an intensive prefix (see com-), + fortis "strong" (see fort). Change of -n- to -m- began in English 14c.