In what follows, you can first reduce to the case of $p$-groups as you intended to, but it is not necessary.
I owe essentially this proof to user:Marvoir, who wrote a solution on French Wikiversity (I slightly simplified it, hopefully not introducting any mistake).
Let $a_1\mid a_2\mid\dots\mid a_r$ be the invariant factors of $G$, so $a_i>1$ and $G\cong(\Bbb Z/a_1\Bbb Z)\times(\Bbb Z/a_1\Bbb Z)\times\dots\times(\Bbb Z/a_r\Bbb Z)$, and similarly, let $b_1\mid\dots\mid b_s$ be those of $H$.
For every natural number $k$, the number of elements $x\in G$ such that $kx=0$ is the sum, over all divisors $d\ge1$ of $k$, of the numbers of elements of order $d$ in $G$. On the other hand, it is equal to $\prod_{i=1}^r\gcd(k,a_i)$. Therefore (since $G,H$ have the same number of elements of any order $d$):
$$\forall k\in\Bbb N\quad\prod_{i=1}^r\gcd(k,a_i)=\prod_{j=1}^s\gcd(k,b_j).$$
Assuming wlog $s\le r$, we shall derive by induction on $s$ that this condition implies $r=s$ and $\forall i\quad a_i=b_i$.
Base case $s=0$: then $r=0$, because if $r\ge1$ then $1=\prod_{i=1}^r\gcd(a_1,a_i)=a_1^r\ge a_1>1$, contradiction.
Induction step: assuming the result holds when $s=S-1$ for some $S\ge1$, let us prove it still holds when $s=S$. So, let $1<a_1\mid a_2\mid\dots\mid a_R$ and $1<b_1\mid b_2\mid\dots\mid b_S$, with $S\le R$, be such that $$\forall k\in\Bbb N\quad\prod_{i=1}^R\gcd(k,a_i)=\prod_{j=1}^S\gcd(k,b_j).$$
Then,
$$a_1^R=\prod_{j=1}^S\gcd(a_1,b_j)\mid a_1^S$$
hence $R=S$, and then
$$\forall j\quad\gcd(a_1,b_j)=a_1$$
so that $a_1\mid b_1$, and (similarly, by symmetry) $b_1\mid a_1$, so that by cancellation of the first term of both products, the main hypothesis rewrites
$$\forall k\in\Bbb N\quad\prod_{i=\color{red}2}^R\gcd(k,a_i)=\prod_{j=\color{red}2}^S\gcd(k,b_j)$$
and by induction hypothesis, the result follows.