Magnetic flux through a closed surface is zero because, in simple words, all the field lines that enter the surface exit it.
In electrostatics there are charges that that can be enclosed by a surface, due to which there’s an electric field though the surface which leads to an electric flux. But in magnetism, there are no magnetic charges. Fundamentally, only a magnetic moment is existent, not a magnetic charge.