Although you want to stick to mdframed I propose a solution with tcolorbox because it can provide mdframed+answer behaviour. This solution also uses hyperref for links.
Like answers, tcolorbox offers some commands and environments to define boxes with parts which will be saved on an external file and printed later on.
Following code is a simplification of example in section 8.3 from tcolorbox documentation (something similar can be seen here)
Every ex box will save the solution (text after \tcblower) into an independent file.
Command \tcbstartrecording[file1] will start to save into file1 the name of all exercises to be included into the solutions list.
Command \tcbstoprecording does what it says.
Command \tcbinputrecords[file1] loads all solutions and print them.
About references, I've suposed that every exercise contains a solution and they respective names are references to their solution/exercise.
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}
\NewTColorBox[auto counter]{ex}{+O{}}{%
enhanced, colframe=black, colback=white, sharp corners,
attach title to upper=\par, coltitle=black, fonttitle=\bfseries,
title={Exercise~\ref{solution@\thetcbcounter}},
label={exercise@\thetcbcounter},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}%
{solutions/exercise-\thetcbcounter.tex}},
#1}
\NewTotalTColorBox[auto counter]{\solution}{mm}{%
enhanced, blanker,
coltitle=black, fonttitle=\bfseries,
title={\ref{exercise@\thetcbcounter}},
label={solution@\thetcbcounter},
attach title to upper={\ },
}{\input{#2}}
\begin{document}
% We start to record all tcblower parts in file file1 for later processing
\tcbstartrecording[file1]
%Every exercise box contains an upper part which is processed and
%printed and a lower (solution) which printing is delayed.
%
\begin{ex}
This is an exercise.
\tcblower
This is the solution for first exercise
\end{ex}
\begin{ex}
This is an exercise.
\tcblower
This is the solution for second exercise
\end{ex}
\tcbstoprecording
%previous recorded files with solutions are processed and printed
\tcbinputrecords[file1]
\end{document}

tcolorboxis great :) – NBur Jan 26 '21 at 07:45exandsoldeclare its ownlabelwhich areexercise@\thetcbcounterandsolution@\thetcbcounter, you can use them or change it according your convenience. – Ignasi Jan 27 '21 at 15:55mdframedtotcolorboxshould be not too difficult. You already know where to find help ;-) – Ignasi Jan 27 '21 at 15:57exhas a mandatory parameter which is the label.\begin{ex}{first}-> \ref{first}. My test worked, but I don't know in your work case. – Ignasi Jan 27 '21 at 17:39