It seems to me that this question can be answered in the affirmative using the same idea (of Lovász) as that other question.
For finite groups $G$ and $H$, let $h(G,H)$ be the number of homomorphisms and $s(G,H)$ the number of surjective homomorphisms from $G$ to $H$.
Consider a finite group $H$. Let $H_1,\dots,H_n$ be all the proper subgroups of $H$ (or just the maximal ones). For $I\subseteq[n]=\{1,\dots,n\}$ let $H_I=\bigcap_{i\in I}H_i$ if $I\ne\emptyset$, and let $H_\emptyset=H$. By the in-and-out principle, for any finite group $G$ we have$$s(G,H)=\sum_{I\subseteq[n]}(-1)^{|I|}h(G,H_I).$$Therefore, if $G_1$ and $G_2$ are finite groups such that $h(G_1,H)=h(G_2,H)$ for every finite group $H$, it follows that $s(G_1,H)=s(G_2,H)$ for every finite group $H$, and in particular that $s(G_1,G_2)=s(G_2,G_2)\ge1$ and $s(G_2,G_1)=s(G_1,G_1)\ge1$, whence $G_1\cong G_2$.