We model the dust and free-free continuum emission in the high-mass star-forming region Sagittarius B2 in order to reconstruct the three-dimensional density and dust temperature distribution, as a crucial input to follow-up studies of the gas velocity field and molecular abundances. We employ the three-dimensional radiative transfer program RADMC-3D to calculate the dust temperature self-consistently, provided a given initial density distribution. This density distribution of the entire cloud complex is then recursively reconstructed based on available continuum maps, including both single-dish and high-resolution interferometric maps covering a wide frequency range (40 GHz - 4 THz). The model covers spatial scales from 45 pc down to 100 au, i.e. a spatial dynamic range of 10^5. We find that the density distribution of Sagittarius B2 can be reasonably well fitted by applying a superposition of spherical cores with Plummer-like density profiles. In order to reproduce the spectral energy distribution, we position Sgr B2(N) along the line of sight behind the plane containing Sgr B2(M). We find that the entire cloud complex comprises a total gas mass of 8.0 x 10^6 Msun within a diameter of 45 pc, corresponding to an averaged gas density of 170 Msun/pc^3. We estimate stellar masses of 2400 Msun and 20700 Msun and luminosities of 1.8 x 10^6 Lsun and 1.2 x 10^7 Lsun for Sgr B2(N) and Sgr B2(M), respectively. We report H_2 column densities of 2.9 x 10^24 cm^-2 for Sgr B2(N) and 2.5 x 10^24 cm^-2 for Sgr B2(M) in a 40 beam. For Sgr B2(S), we derive a stellar mass of 1100 Msun, a luminosity of 6.6 x 10^5 Lsun and a H_2 column density of 2.2 x 10^24 cm^-2 in a 40 beam. We calculate a star formation efficiency of 5% for Sgr B2(N) and 50% for Sgr B2(M), indicating that most of the gas content in Sgr B2(M) has already been converted to stars or dispersed.