Exoplanet observations have revealed that some giant planets contain large metal masses. The metal-mass fraction is typically higher than 0.1. HD 149026b and TOI-849b are characteristic examples of these giant planets. We investigate the formation of giant planets in a rapidly dissipating disk using N-body simulations that include pebble accretion. We find that the metal mass in giant planets can increase after giant impacts between planets with the pebble isolation mass. Further, we demonstrate that the runaway gas accretion can be quenched by a rapid disk clearing due to photoevaporation, resulting in the formation of giant planets with large metal fractions. In conclusion, the large metal mass and metal-mass fraction of the observed giant planets, including HD 149026b and TOI-849b, can be naturally explained without relying on the impact erosion after the substantial depletion of the disk gas and other envelope-loss mechanisms.