Gamma-ray emission in active galaxies is likely produced within the inner jet, or in the close vicinity of the supermassive black hole (SMBH) at sub-parsec distances. Gamma rays have to pass through the surrounding massive stellar cluster which luminous stars can accidentally appear close to the observers line of sight. In such a case, soft radiation of massive stars can create enough target for transient absorption of the gamma rays in multi-GeV to TeV energy range. We consider the effect of such stellar encounters on the gamma-ray spectrum produced within the massive stellar cluster surrounding a central SMBH. We predict characteristic, time-dependent effects on the gamma-ray spectra due to the encounter with the single luminous star and also stellar binary system. We conclude that during the encounter, the gamma-ray spectrum of an active galaxy should steepen at tens of GeV and harden in the range of hundreds of GeV. As an example, we consider such effects on the spectra observed from a typical blazar, 1ES 1959+650 (in an active state) and also in the case of a radio galaxy M87 (in a low state). It is shown that observation of such transient characteristic features in the gamma-ray spectra, observed from blazars and radio galaxies, lays within the sensitivity of the future Cherenkov Telescope Array.