One thing we must remember is that the potential energy of an object doesn't have any significance. The only thing that matters is the difference in potential energies between two points on the trajectory. That said, we know that this difference is given by $U(b) - U(a) = -\int_{a}^{b}\vec{F} \cdot \vec{dr}$ , where a and b are the initial and final points, $\vec{F}$ is a conservative force. Now, in your case, if we take downward direction as -ve, then $\vec{F} = -mg\hat{y}$ and the $\vec{dr} = -ld\theta(cos\theta\hat{x} + sin\theta\hat{y})$. Dot product gives $mglsin\theta d\theta$. This is to be integrated from $90^{\circ}$, the initial position to $\theta$, the final position. This gives $mglcos\theta.$ But beware! We haven't included the minus sign from the definition of potential energy, which gives the right answer.
Edit:
Okay I just realised I made a slight mistake: integral of $sin\theta$ is $-cos\theta$, which will lead to the erroneous result you have obtained. The reason is that when we formulate potential, we always talk about it as the work done by an external force in causing the displacement, WITHOUT causing an acceleration to the object, and moving against the force field. Here, it is all about looking at the same displacement, but in order that the acceleration be zero, the force must be $\vec F = mg\hat y$.
On a final note, the formula for potential energy difference is $U(b) - U(a) = \int_{a}^{b}\vec{F_E} \cdot\vec{dr}$ where all the variables are the same as before, except that the subscript E denotes the external force required, which is negative of gravitational force, and $\vec{dr} = ld\theta(cos\theta)\hat{x} + ld\theta(sin\theta)\hat{y}$, as this is the displacement against the force field. Setting $a = \theta$ and $b = \pi/2$ and using the assumption that $U(\pi/2)=0$ gives the answer. :)