TL;DR: Briefly, the answer is that Jesus knew he was the Son of God from all eternity, and his human intellect was aware of that fact from the moment of the Incarnation. Mary did not have to tell him; Jesus, rather, would have needed to tell her.
The Hypostatic Union and the Incarnation
The reason that Jesus would have had to know his identity stems fundamentally from something called the Hypostatic Union.
As readers may recall, the Catholic Church (and the Orthodox Churches, as well as any church that accepts the Council of Chalcedon) teaches that Jesus Christ is fully God and fully man. He possesses two natures—human and divine—“without confusion, change, division or separation” (Council of Chalcedon, DS 302). However, He is a unique Person or Hypostasis: namely, the Divine Son. (See Catechism of the Catholic Church 467-469).
The important thing to understand about the Hypostatic Union is that no closer union between human and divine nature is possible: not even our definitive union with God that we will enjoy in Heaven, when we see Him “face to face” (1 Cor. 13:12).
The human and divine natures—although distinct—are so closely united that all of Jesus’ actions, even his human ones, are actions of God, and likewise, the actions requiring Divine power are rightly said to be produced by the man Jesus.
It is perfectly correct to say, for example, “Jesus created the universe” and “God died on the Cross.”
Affirming that Jesus is a human person, in addition to a Divine Person, would be tantamount to the heresy of Nestorianism. In reality, he is a Divine Person only (CCC 466).
Moreover, since Jesus is fully man (in addition to being fully God), his human nature must possess all of the characteristics of human nature: in particular, he must have a human intellect and a human will (CCC 470-474).
Note that once a human being comes into existence (i.e., at his conception), he already possesses a complete human intellect and will. He is unable to exercise that intellect and will until his brain and cognitive apparatus are ready for it, but he possesses them from the beginning.
It follows that Jesus possessed a human intellect from the moment of his Incarnation. (See Summa theologiae, I, q. 77, for an overview of how the intellect and will—the “powers” of the soul—relate to the human soul they belong to.)
Jesus’ three manners of obtaining human knowledge
As a consequence of the Hypostatic Union, and the fact that Jesus has possessed a human intellect from the moment of his conception, it seems an inevitable conclusion that Jesus in his human intellect must have enjoyed the Beatific Vision—that is, he must have seen God face-to-face—as soon as he became incarnate. This idea is accepted in Pope Pius XII’s encyclical Mystici Corporis No. 75:
For hardly was He conceived in the womb of the Mother of God, when He began to enjoy the Beatific Vision, and in that vision all the members of His Mystical Body were continually and unceasingly present to Him, and He embraced them with His redeeming love.
Pope Pius, in turn, took that idea from St. Thomas Aquinas’ Summa theologiae, III, q. 9, a. 2, in which Aquinas argues that Christ did indeed have the knowledge proper to the Blessed in Heaven. Aquinas adds another argument (an argument of fittingness) for Christ to have the knowledge of the Blessed:
it was necessary that the beatific knowledge, which consists in the vision of God, should belong to Christ pre-eminently, since the cause ought always to be more efficacious than the effect (from the responsum).
In other words, because Jesus Christ in his human nature is the cause of our salvation, it is fitting that he should experience all of the effects of that salvation before we do (for the same reason that Christ rose and ascended in to Heaven before our own resurrection).
Hence, although it has never been formalized in a dogma, the idea that Jesus Christ possessed the Beatific Vision throughout his life has the status of a “probable opinion”: denying it comes perilously close to affirming a Nestorian separation of Jesus’ human nature from his Divine Nature.
In addition to his Beatific Vision, Jesus would have had all of the infused knowledge necessary for him to accomplish his mission. In III, q. 9, a. 3, Aquinas argues that because Jesus was perfect man (as taught by the Council of Chalcedon), his human intellect would have had knowledge of all the things actually created by him (whether in the past, in present, or in the future). This is, again, a consequence of being hypostatically united to the Divine Word: since Jesus simply is the Person of the Son, he must possess all the knowledge that the Son has (at least as much of it as a human intellect can hold).
As FMS points out in his post, this teaching has essentially been taken up by the Catechism in number 473:
But at the same time, this truly human knowledge of God's Son expressed the divine life of his person. “The human nature of God’s Son, not by itself but by its union with the Word, knew and showed forth in itself everything that pertains to God” [quoting Maximus the Confessor, PG 90, 840A]. Such is first of all the case with the intimate and immediate knowledge that the Son of God made man has of his Father.
Finally, Jesus would have been able to learn things in the way that all human beings learn: by experience, using his senses and cognitive apparatus (i.e., his brain). (That is the argument of III, q. 9, a. 4.) This was the only type of human knowledge that would have required Jesus to be developed in body. It was in this capacity that Jesus (like all of us) learned to talk, learned to recognize his mother and (foster) father, experienced the sunrise, went to school, and so on.
Regarding Jesus’ knowledge of his identity
It follows that, thanks to the infused knowledge that Jesus possessed, he already had full human knowledge of his identity (that is, of his divinity) from the moment of his conception. Obviously, he had no way of expressing that knowledge until his brain and speech were sufficiently developed, but he must have had that knowledge right from the beginning.
(Note that Jesus probably experienced “consciousness” in the same way we do: but as soon as his cognitive functions were in order, he would have been aware of his Hypostatic Union and hence of his identity.)