ذَلِكَ مِنْ أَنبَاء الْغَيْبِ نُوحِيهِ إِلَيكَ
This is a part of the news of the Ghayb which We reveal.
"and narrate to you (O Muhammad),"
وَمَا كُنتَ لَدَيْهِمْ إِذْ يُلْقُون أَقْلَمَهُمْ أَيُّهُمْ يَكْفُلُ مَرْيَمَ وَمَا كُنتَ لَدَيْهِمْ إِذْ يَخْتَصِمُونَ
You were not with them, when they cast lots with their pens as to which of them should be charged with the care of Maryam; nor were you with them when they disputed.
meaning, "You were not present, O Muhammad, when this occurred, so you cannot narrate what happened to the people as an eye witness. Rather, Allah disclosed these facts to you as if you were a witness, when they conducted a lottery to choose the custodian of Maryam, seeking the reward of this good deed."
Ibn Jarir recorded that Ikrimah said,
"Maryam's mother left with Maryam, carrying her in her infant cloth, and took her to the rabbis from the offspring of Aaron, the brother of Musa. They were responsible for taking care of Bayt Al-Maqdis (the Masjid) at that time, just as there were those who took care of the Ka`bah.
Maryam's mother said to them, `Take this child whom I vowed (to serve the Masjid), I have set her free, since she is my daughter, for no menstruating woman should enter the Masjid, and I shall not take her back home.'
They said, `She is the daughter of our Imam,' as Imran used to lead them in prayer, `who took care of our sacrificial rituals.'
Zakariyya said, `Give her to me, for her maternal aunt is my wife.'
They said, `Our hearts cannot bear that you take her, for she is the daughter of our Imam.' So they conducted a lottery with the pens with which they wrote the Tawrah, and Zakariyya won the lottery and took Maryam into his care."'
Ikrimah, As-Suddi, Qatadah, Ar-Rabi bin Anas, and several others said that;
the rabbis went into the Jordan river and conducted a lottery there, deciding to throw their pens into the river. The pen that remained afloat and idle would indicate that its owner would take care of Maryam.
When they threw their pens into the river, the water took all the pens under, except Zakariyya's pen, which remained afloat in its place.
Zakariyya was also their master, chief, scholar, Imam and Prophet, may Allah's peace and blessings be on him and the rest of the Prophets