Fish also stay near the surface when there is ammonia and/or nitrite in the water. Ammonia burns their gills, making it harder for them to take up oxygen; nitrite binds to the blood cells so the oxygen can't attach. These both make the fish feel as though it's short of oxygen so they go to the surface where the oxygen level is higher.
Both ammonia and nitrite occur in the water during a fish-in cycle which is why daily water changes are needed to keep them as near to zero as possible.