I don't understand how bending moment is being calculated in the problem mentioned in the above link. I understand the parts after that i.e. calculating elastic potential and partial derivatives but I am just not able to understand the bending moment part. I would be glad to receive some responses on this.
When you constrain a beam end to point in the same direction as the clamped end (which is the case with the lower beams), then you have to apply a moment equal to $Ql$ (where $Q$ is the end load and $l$ is the length) to maintain this angle. You'll note that this results in an end-moment magnitude of $Ql$ at each end, which produces symmetric curvature.
In this case, though, $Q_1$ is split between two beams, so the applied end moment for each is $Q_1l_1/2$, and the moment along each is $-Q_1x+Q_1l_1/2$.
No such end moment is needed in the free beam on top, so the moment along that beam is simply $-Q_2x$.
Is this what you're asking about?