The potential energy of a dipole is the energy possessed by the dipole by virtue of its particular position in the electric field.
Let an electric dipole of moment \vec p is placed in a uniform external electric field \vec E making an angle \theta with the field direction. Thus, torque \tau acting on the dipole is
\tau = pE\sin\theta
The torque tries to set the dipole along \vec E. Small amount of work done dW in rotating the dipole through an angle d\theta against the torque is given by,
dW= \tau d\theta=pE\sin\theta d\theta
\therefore Total workdone in rotating the dipole from \theta_1 and \theta_2 is
W= \int^{\theta_2}_{\theta_1}pE\sin\theta d\theta=pE[\cos\theta]^{\theta_1}_{\theta_2}=-pE(\cos\theta_2-\cos\theta_1)
Potential energy of the dipole, U=W=-pE(\cos\theta_2 -\cos\theta_1)