The 'trick' works because gases (like air) expand very considerably on heating, roughly in accordance with the Ideal Gas Law. Also, the shape of the pipette helps because the hot water in the top part (after having turned the pipette upside down) takes time to flow to the lower part. This means there's usually enough water in the top part be be expelled by the heating, expanding air.