It is unclear when it happened. There is neither a clear hadith nor a verse. There are different views regarding it:
It was an independent incident that occurred because Ibrahim (AS) saw a dead body of a person or animal being torn apart and eaten by scavengers. This prompted him to ask Allah the question. This opinion is narrated from Al-Hasan, Qatadah, and Adh-Dhahhaak. This appears to be the correct view.
This view has no time frame attached to it nor any reference to other events. So, it does not help in telling us when Ibrahim (AS) asked it.
It was said in the argument with the king in 2:258. This view states that Ibrahim (AS) asked Allah to show the king and the people a miracle to prove that Allah can bring people to life.
To know the time that this view would entail, it requires knowing when the argument with the king happened in relation to being saved from the fire.
It was said after the argument with the king in 2:258. This view states that Ibrahim (AS) asked Allah for the miracle for his own heart's satisfaction after he already argued with the king about the topic. To move from only understanding it in a theoretical sense to understanding it in practice.
As stated before, we need to know when the argument with the king happened. There are two opinions:
That it was after he was saved from the fire. This is narrated from As-Suddi. This seems to be more correct.
That it was before he was put into the fire and after he broke the idols. This was narrated from Muqatil.
As for why Ibrahim (AS) asked it, it wasn't because he doubted. Ibrahim (AS) was already a complete believer and a Prophet. Rather, he wanted to understand better and see with his own eyes how Allah brings the dead to life.
It isn't strange that Allah shows prophets miracles to strengthen their faith. After all, they go through the biggest troubles, so they need the strongest faith to bear the tests.
Allah says about our Prophet (SAW): "He certainly saw of the greatest signs of his Lord." (53:18) referring to him seeing Jibril (AS)'s true form.
Allah says to Musa (AS): "That We may show you [some] of Our greater signs." (20:23)