It's possible it appears elsewhere, but I found it in the Adbhuta Ramayana:

यदा यदा हि धर्म्मस्य ग्लानिर्भवति सुव्रत । अभ्युत्थानमधर्मस्य तदा प्रकृति सम्भवः ।। 18 ।।
yada yada hi dharmmasya glänirbhavati suvrata ! /
abhyutthanamadharmasya tada prakṛti sambhavaḥ //
Oh, the keeper of righteous vows! Whenever righteousness, propriety, probity and virtuousness (collectively called 'Dharma') is on the decline, when those who follow Dharma are tormented and there is an upswing or upsurge of evil and sinful forces, the cosmic mother known as Nature or Prakṛti manifests herself to reverse this evil trend, and instead, she rejuvinates, enhances, redeems and reinstates those virtues which make a comeback in their original glorious and exalted form (18).