Given a symmetric positive-definite matrix $M$, find all $A$ such that $A^\top M A=M$

The matrix $D$ has a positive square root, name it $E$, which is a diagonal matrix with entries $\sqrt{d_i}>0$. Hence $E^{\top}=E$ and $(E^{-1})^{\top}=E^{-1}$.

From $B^{\top}DB=D$ we obtain $E^{-1}B^{\top}E=EB^{-1}E^{-1}$, hence $$ (EBE^{-1})^{\top}=(E^{-1})^{\top}B^{\top}E^{\top}=EB^{-1}E^{-1}=(EBE^{-1})^{-1}. $$ So $EBE^{-1}$ is an orthogonal (unitary) matrix.

Starting from any orthogonal (unitary) matrix $C$ we set $B:=E^{-1}CE$. Then $B^{\top}=E^{\top}C^{\top}(E^{-1})^{\top}=EC^{\top}E^{-1}$ and so $$ B^{\top}DB=B^{\top}EEB=EC^{\top}E^{-1}EEE^{-1}CE=EC^{\top}CE=E^2=D. $$