Let $G$ be a $p$-group of nilpotency class at most 2, where $p$ is an odd prime. Then ${\{x^p| x \in G\}}$ is a subgroup of $G$. Let $G$ be a $p$-group of nilpotency class  at most 2, where $p$ is an odd prime.Then $A=\{{x^p| x \in G}\}$ is a subgroup of $G$.
As $G$ is a group of nilpotency class at most 2, if the nilpotency class of $G$ equal 1 then $G$ is an abelian group and the result is obvious.
Now we consider the nilpotency class equal 2. Then we have $G^\prime \leq Z(G)$. 
Also as a result for every natural number like $n$ and every $x,y$ in $G$ we have 
$$(xy)^n= x^ny^n[y,x]^{n\frac{n-1}{2}}.$$
Now I consider $x^p, y^p$ in $A$ where $x,y$ are in $G$. I want to show $x^py^p$ in $A$. According to the above fact we have
$$ (xy)^p= x^py^p[y,x]^{p\frac{p-1}{2}}.$$

I want to show $[y,x]^{p\frac{p-1}{2}}=1$, but I don't know how.

 A: You won’t be able to prove the commutator is trivial, because it doesn’t have to be.
Consider the multiplicative group of upper triangular $3\times 3$ matrices with entries in $\mathbb{Z}/p^n\mathbb{Z}$ with $1$s in the diagonal. Multiplication is given by
$$\left(\begin{array}{ccc}
1&a&c\\
0 & 1 & b\\
0 & 0 & 1\end{array}\right) \left(\begin{array}{ccc}
1 & \alpha & \gamma\\
0 & 1 & \beta\\
0 & 0 & 1
\end{array}\right) = \left(\begin{array}{ccc}
1 & a+\alpha & c + \gamma + a\beta\\
0 &1 & b+\beta\\
0 & 0 & 1\end{array}\right).$$
This is a nilpotent group of order $p^{3n}$ and class $2$. The commutator subgroup is generated by the matricx with $a=b=0$, $c=1$. 
If you let $x$ be the matrix corresponding to $a=1$, $b=c=0$; and $y$ the matrix corresponding to $a=c=0$, $b=1$, then $[x,y]=x^{-1}y^{-1}xy$ is the matrix with $a=b=0$, $c=1$, which has order $p^n$. In particular, if $n\gt 1$, then $[x,y]^{p(p-1)/2}\neq e$.
However, it is not necessary to show the commutator is trivial. As you already computed, in a nilpotent group of class at most $2$, we have
$$ (xy)^n = x^n y^n [y,x]^{n(n-1)/2}.$$
So, to show that $A$ is a subgroup, we note that $e^p=e\in A$; that if $x^p\in $, then $(x^p)^{-1} = (x^{-1})^p\in A$. And then the hard one: showing that the product of two $p$th powers is a $p$th power. Suppose $x^p,y^p\in A$. Then, as previously computed we have:
$$\begin{align*}
(xy)^p &= x^py^p[y,x]^{p(p-1)/2}\\
(xy)^p[x,y]^{p(p-1)/2} &= x^py^p\\
(xy)^p\left([x,y]^{(p-1)/2}\right) &= x^py^p.
\end{align*}$$
To finish off, we note that since $[x,y]^{(p-1)/2}$ commutes with $xy$ (in fact, it is central), then
$$ \left( (xy)[x,y]^{(p-1)/2}\right)^p = (xy)^p[x,y]^{p(p-1)/2} = x^py^p.$$
Thus, $x^py^p$ is equal to a $p$th power, and hence lies in $A$. Thus, $A$ is closed under products, and we are done.
