This effect is called capillarity and is not that straightforward.
The contact between water and a solid surface is determined by the chemical bonds. It is macroscopically observed in the contact angle that the water/air surface makes with the solid surface. This angle depends on the strength of the bonds between the solid and the water molecules. You can see this when you pour water in a glass: the water at the edge of the glass is a bit higher than in the center; it makes an angle with the glass surface.
Now, if there is a lot of solid around the water, such as water in a tiny tube, there are a lot of contact points. Therefore, the water/air interface will be strongly curved. The curvature of this interface modifies the surface tension, which represents the energy contained in that surface. A good way to interpret the effect of curvature is that you surround a given portion of the interface by more (or less) water molecules as you curve the interface. The pressure on the interface is thus reduced or increased depending on the curvature.
In a small vertical tube, the curvature can be such that the pressure is higher than for a flat interface. Thus, it can counteract the gravity more easily.
In conclusion, the energy comes from the thermal (pressure) energy of the water molecules which push from the bottom.