I don't know what you mean by "discovered". Ganesha is mentioned in Vedas, as described in this answer. So, He is eternal and He manifests differently in different Kalpas.
Chapter 13 Rudra Samhita (Kumara Khanda) of Shiva Purana says
कल्पभेदाद्गणेशस्य जनिः प्रोक्ता विधेः परात्।
शनिदृष्टं शिरश्छिन्नं सञ्चितं गाजमाननम्॥५॥
Brahma said, “Because of the difference in the kalpas, there are
several stories of the birth of Ganesa. Once, because of Saturn's
casting his gaze over Ganesa, the latter's head was cut off and it was
replaced with the head of an elephant.
यत्र छिन्नं शिरस्तस्य शिवेन च कृपालुना।। ६॥
Presently, the story of the birth of Ganesa in the Sveta kalpa is
being narrated, wherein, the compassionate Šiva, had himself cut off
So, it's clear Ganesha is eternal (as He is mentioned in Vedas) and He manifests differently in different Kalpas. Each Purana describes stories of a particular kalpa which i discuss here and Shiva Purana discusses stories of Sveta kalpa.
Similarly, Chapter 23 Part 1 of Varaha purana mentions different birth story of Ganesha. Varaha Kalpa mentions mainly stories of "Manava Kalpa".
11-15. How is it not there in sky ?" So thinking he laughed. By the
power of knowledge, by what was seen by Rudra in the sky, by what was
said by Brahma formerly as the body of all beings, by the laughter of
Rudra, by all these four in the four elements Earth and the rest,
there arose a boy effulgent with glowing face, looking like Rudra
himself and stupefying Devas with the charm of his dazzling
Seeing the person of this great boy, Uma remained with eyes stuck on him.
Looking at the beautiful form of the boy and thinking of the fickleness of women's minds Rudra got angry and cursed him.
"O boy, you will attain an elephant's face, protruding belly and yajnopavlta of serpents".
Coming to Sweta Varaha Kalpa (Current Kalpa), we do not know exactly in which Mahayuga He was born.
Parvati was born just after beginning of Vaivasvara Manvantara in Swetha varaha Kalpa as mentioned in this chapter of Skanda Purana. But Ganesha is elder brother of Skanda. He might be born just after Marriage of Shiva and Parvati.
Ramayana happened during 24th treta yuga and Mahabharata happened during 28th Dwapara Yuga as mentioned in this answer. There is huge difference in time span of Ganesha's birth and Mahabharata or Ramayana.
Moreover, it was Ganesha who wrote Mahabharata when Krishna Dwaipayana Vyasa narrated, as mentioned in Adiparva of Mahabharata.
So, Ganesha existed and well known even during times of Mahabharata and Ramayana.