The problem seems to be arising due to mercury-mode being defined as a major-mode rather than a different system under prolog mode. I changed a line in prolog-font-lock-keywords to work with mercury-mode and added a call to prolog-mode-variables in define-derived-mode mercury-mode to setup the required variables for font lock keywords.