How to make page numbering continuous after a second \mainmatter?

This depends highly on the document class you're using. Taking the standard book as base, \fontmatter and \mainmatter both reset the page counter. \backmatter on the other hand doesn't:

\newcommand\frontmatter{%
    \cleardoublepage
  \@mainmatterfalse
  \pagenumbering{roman}}
\newcommand\mainmatter{%
    \cleardoublepage
  \@mainmattertrue
  \pagenumbering{arabic}}
\newcommand\backmatter{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
  \@mainmatterfalse}

The important thing that causes the page numbering to be reset when issuing the above commands is \pagenumbering (taken from latex.ltx):

\def\pagenumbering#1{%
  \global\c@page \@ne \gdef\thepage{\csname @#1\endcsname
   \c@page}}

As mentioned, this sets the page counter to one (\global\c@page \@ne) and redefines the representation \thepage (\gdef\thepage{\csname @#1\endcsname\c@page}). So, with an issue of \pagenumbering{roman} (as is performed by \frontmatter), LaTeX issues something equivalent to

\setcounter{page}{1}
\renewcommand{\thepage}{\arabic{page}}

memoir does something similar. If your question is only "How to prevent LaTeX from resetting the page counter?", then you should not use \frontmatter and friends, or at least void what \pagenumbering is doing. You might just be better off redefining the page counter printing mechanism \thepage as is done above. Or, for convenience of use in order to stick to your current layout, perhaps the following suffices in your preamble (which removes the page counter resetting):

\makeatletter
\def\pagenumbering#1{%
  \gdef\thepage{\csname @#1\endcsname \c@page}}
\makeatother

Finally, neither book nor memoir resets the page counter for \backmatter.


An easy solution to your problem is getting rid of the \pagenumbering command issued by \mainmatter, which is responsible of resetting the page number. So I propose

\makeatletter
\def\mainmatter{%
  \cleardoublepage
  \@mainmattertrue
  \pagenumbering{arabic}
  \def\mainmatter{\cleardoublepage\@mainmattertrue}
}
\makeatother

The first call of \mainmatter would do as usual, so the page numbering will be arabic, starting from 1. Then it redefines itself to issue only \cleardoublepage and \@mainmattertrue. The latter restores chapter numbering.