The angels questioned Allah in order to find out and learn
Question

How can we reconcile between the verse {He is not questioned about what He does, but they will be questioned} [al-Anbiya’ 21:23] and the verse {And [mention, O Muhammad], when your Lord said to the angels, “Indeed, I will make upon the earth successive generations.” They said, “Will You place upon it those who cause corruption therein and shed blood, while we declare Your praise and sanctify You?”} [al-Baqarah 2:30]?
Praise be to Allah.Firstly:
Regarding the angels’ questioning of Allah that is mentioned in Surat al-Baqarah, it was a question aimed at finding out and seeking to understand and learn something new. It was not an importunate question or an objection, for they are far above such a thing, may Allah’s peace be upon all of them. And it was said that Allah, may He be exalted, gave them permission to ask, so they asked.
Makki said in al-Hidayah (1/216-217): Many of the commentators narrated that the angels were aware that the jinn who dwelt on earth had caused a great deal of mischief and shed blood, so they asked by way of wanting to learn the reason and seeking new knowledge: {Will You place upon it one who causes corruption therein and sheds blood?} In other words, will they be like those who spread mischief?
Thus they asked for the purpose of seeking to learn, not to object, because they had no knowledge of how these successive generations would be, which Allah told them He was going to create. It was said that they said that by way of wondering, as the Arabs say: Will you show kindness to So-and-so, when he mistreats you?
And it was said that Allah, may He be exalted, gave them permission to ask about that.
And it was said that Allah, may He be exalted, mentioned that by way of informing them that He was going to create successive generations on earth, so they asked by way of seeking knowledge: What are those successive generations going to be like? So He said: He [Adam] will have progeny who will spread mischief on earth and shed blood, whereupon they said by way of astonishment and seeking confirmation, not by way of objecting: “Will You place upon it those who cause corruption therein and shed blood?”
And it was said that they said that by way of astonishment at what Allah told them about those successive generations causing mischief on earth and shedding blood. End quote.
Al-Wahidi narrated in al-Basit (2/324) the views of scholars in response to that, as he said:
Some of the commentators said that there is an implicit meaning that is stated briefly. What it means is: are You going to place in it those who will spread corruption in it [and shed blood? Or are You going to place in it those who will not spread mischief in it] or shed blood? This is like the verse in which Allah, may He be exalted, says: {Is one who is devoutly obedient during periods of the night …?} [az-Zumar 39:9], meaning: is he like one who is not devoutly obedient? And it is like the words of the poet Abu Dhu’ayb:
I went against my gut instinct, for I am obedient to her command, and I do not know whether pursuing her is wise.
What he meant is: is it wise or foolish?
Based on that, the angels were seeking to understand and perhaps be told something of which they were not aware; it was not their intention to disapprove or object. Then Allah said: {“Indeed, I know that which you do not know”} [al-Baqarah 2:29]. He did not inform them about the character of the sons of Adam and He did not explain to them whether He wanted to create someone who would spread mischief or would not spread mischief.
It was said that when Allah said {“Indeed, I will make upon the earth successive generations”}, the angels did not know what type of creatures those successive generations would be. They said: O our Lord, will You place on the earth successive generations who will spread mischief like the children of the jinn? Or will You make successive generations from among the angels? For we glorify and praise You. But Allah did not inform them about that, and he said: {“Indeed, I know that which you do not know”} – in other words: among them will be both those who are obedient and those who are disobedient.
Az-Zajjaaj said, narrating from someone else: what is meant here is that Allah informed the angels that He was going to place on the earth successive generations, and the successive generations were a group of the sons of Adam who would shed blood. Allah gave the angels permission to ask Him about that, and His informing them of that served to further confirm to them that He knows the unseen. It is as if they said: Are You going to create in it people who will shed blood and disobey You? Rather what they should do when they realise that You have created them is glorify and praise You as we do, and sanctify You as we do. They only said that when they were given permission to ask, because Allah, may He be exalted, has described them as doing what they are commanded. End quote.
Ibn Kathir (1/216) said: What the angels said was not by way of objecting to Allah, or by way of envy for the sons of Adam, as some commentators thought. Allah, may He be exalted, has described the angels as {They do not speak until He has spoken} [al-Anbiya’ 21:27]; in other words, they do not ask Him about anything for which they are not given permission.
When Allah told them that He was going to create a new creation on earth, Qatadah said: They already knew that they [the new creation] would spread mischief on earth, so they said: {“Will You place upon it…”}. That was a question by way of enquiring and finding out the wisdom behind that. They said: O our Lord, what is the wisdom behind creating these people, even though some of them will spread mischief on earth and shed blood? For if the purpose is that they should worship You, we already glorify and praise You and sanctify You. In other words, we pray to You and we will not do anything of that manner at all, so why did You not decree that we are enough?
Allah, may He be exalted, said, responding to this question of theirs: {“Indeed, I know that which you do not know”}. That is, I know that there is a greater purpose in creating such people, which outweighs the mischief that you refer to, which you do not know, for there will be Prophets and Messengers among them, and among them there will be those who are strong and true in faith, martyrs, righteous people, devoted worshippers, ascetics, close friends of Mine, devout people, those who are close to Me, scholars who act in accordance with their knowledge, those who fear Me, those who love Me, and those who will follow the Messengers (blessings and peace of Allah be upon them). End quote.
Secondly:
With regard to the verse in which Allah, may He be exalted, says: {He is not questioned about what He does, but they will be questioned} [al-Anbiya’ 21:23], it is similar to the verse {Indeed, Allah does what He wills} [al-Hajj 22:13].
What is meant is that Allah, may He be exalted, is the One Who controls His creation in whatever way He wills, for His is the power of creation and His is the command, and it is He Who is in control. Just as He creates them however He wills, blesses whomever He wills, dooms whoever He wills, grants good health to whomever He wills, decrees sickness for whomever He wills, supports whomever He wills, and forsakes whomever He wills, in like manner He rules over His slaves however He wills. Therefore He permits whatever He wills, forbids whatever He wills, allows whatever He wills, and proscribes whatever He wills, for when Allah decrees [a thing], none can reverse His decree."(Tafsir Ibn Kathir  1/387).
It says in Tafsir al-Wasit – Majma‘ al-Buhuth (6/1102): This is an elaboration on the fact that He, may He be glorified, is one and unique in His divinity and majestic in His Lordship, and in the fact that He questions His slaves about what they do, but He is not to be questioned by them about what He does to them. The great scholar az-Zamakhshari said, commenting on this verse: As it is the case that usually subjects do not question kings about their actions or the way in which they run the affairs of their kingdoms, out of respect and awe, despite the fact that kings are susceptible to error, mistakes and all kinds of mischief, by the same token it is more appropriate that the King of Kings and Lord of Lords, their Creator and Provider, is not to be questioned about what He does, especially when it is known and well established that everything He does is rational and based on wisdom, so it is not possible for Him to make mistakes or do anything abhorrent. End quote.
As for people, they will be questioned, because they are His slaves and are obliged to obey Him and act in accordance with His laws which He prescribed for them on the lips of His Messengers, and because of what He has given them of sound reason, which enables them to distinguish truth from falsehood, good from evil, and benefit from harm.
Among those of His slaves whom He will question are those whom people associate with Him, such as the Messiah and the angels; so how can those whom they worshipped be deserving of that worship when they too will be questioned by the One God, may He be glorified and exalted? End quote.
Conclusion:
Allah, may He be exalted, may be asked for the purpose of learning with humility, or He may be asked for the purpose of seeking guidance and knowledge, but He is not to be questioned by way of objection or disapproval of anything that He does, may He be glorified.
The question of the angels was a question that was aimed at learning, for they were aware of the rights of their Lord, and they glorified and praised Him, for they are the ones who do not disobey Allah in what He commands them, and they do as they are commanded.
And Allah knows best.