The invention discloses a device for preparing a fertilizer by using excess sludge and agricultural and forestry waste. The device comprises a mixing system, a fermentation composting system, a screening system, a granulation and drying system, an exhaust gas treatment system, and a wastewater backflow system. Compared with the prior art, by using water, aerobiotic microorganisms and anoxic microorganisms in the excess sludge and cellulose in the agricultural and forestry waste, the device achieves organic matter degradation, pathogenic bacterium killing and heavy metal passivation of the excess sludge and the agricultural and forestry waste through the aerobiotic fermentation, anoxic fermentation and anaerobic fermentation of the excess sludge and the agricultural and forestry waste; the obtained product is thorough in degradation, good in air permeability, high in humus content, comprehensive and rich in nutrient and low in bacterium and virus content, and can serve as a farmland fertilizer or a soil conditioner. The device is simple in structure, stable and reliable in operation, high in production efficiency, and free of odor gas and secondary pollution.