One of the puzzling features of solar magnetism is formation of long-living compact magnetic structures, such as sunspots and pores, in the highly turbulent upper layer of the solar convective zone. We use realistic radiative three-dimensional MHD simulations to investigate the interaction between magnetic field and turbulent convection. In the simulations, a weak vertical uniform magnetic field is imposed in a region of fully developed granular convection, and the total magnetic flux through the top and bottom boundaries is kept constant. The simulation results reveal a process of spontaneous formation of stable magnetic structures, which may be a key to understanding the magnetic self-organization on the Sun and formation of pores and sunspots. This process consists of two basic steps: (1) formation of small-scale filamentary magnetic structures associated with concentrations of vorticity and whirlpool-type motions, and (2) merging of these structures due to the vortex attraction, caused by converging downdrafts around magnetic concentration below the surface. In the resulting large-scale structure maintained by the converging plasma motions, the magnetic field strength reaches ∼1.5 kG at the surface and ∼6 kG in the interior, and the surface structure resembles solar pores. The magnetic structure remains stable for the whole simulation run of several hours with no sign of decay.