Yes. But Hanuman got captured on will, wanting to see who Ravana was. Hanuman was extremely intelligent and knew well that just knowing the map of Lanka was not sufficient; knowing the prowess of your enemy is necessary, too.
Hanuman was strong enough to defeat everyone in Lanka; however, when Indrajit took him captive by discharging a missile presided by Brahma, he submitted to it with respect to Brahma (the grandfather of the entire creation).
Moreover, he considered it an excellent opportunity to learn about Ravana and have a talk with him.
Reflecting on the potency of the missile, the grace of Brahma enjoyed by him and pondering his capacity to undo it, Hanuman submitted to the ordinance of Brahma. He said to himself, “No fear is experienced by me even though I stand bound by means of a missile, protected as I am by Brahma and Indra as well as by the wind god. I discern a great advantage, viz, the opportunity of a talk with Ravana, following my capture at the hands of the ogres. Therefore, let my enemies seize me”.
Canto 48 - Sundara Kanda, Valmiki Ramayana
When Hanuman is brought in front of Ravana, both beheld each other and Hanuman declares he is the messenger who has arrived at Lanka under the command of Sugriva. Hanuman was wonderstruck by Ravana’s extraordinary splendor and his magnificence; he thought in his mind that had the ogre not been evil, he could have even ruled Heaven.
Dazzled by his magnificence on seeing Ravana shining brightly, Hanuman thought in his mind as follows: “What charm, what presence of mind, what courage, what splendor and what combination of all auspicious bodily marks is present in the king of ogres! Had his mighty lord of ogres not been antagonistic to virtue, he would have proved to be a protector of the realm of gods, including Indra. Due to his cruel and ruthless deeds condemned by the world, all people, including gods and demons, remain actually afraid of him.”
Canto 48 - Sundara Kanda, Valmiki Ramayana
Later on, being questioned, Hanuman narrates all his activities on entering Lanka, from destroying the grove to being held captive by Indrajit. He mentions he is the messenger of Shri Ram and states that even though he had the power to get free from the missile presided by Brahma, he submitted because he wanted to see Ravana face to face.
He also warns Ravana that he should send Sita back to Shri Ram if he wishes to survive. If not, then he must start preparing for the worst and his destruction.
“Even though I am undoubtedly capable of destroying Lanka with its horses, chariots and elephants single-handed, such is, however, not the resolve of Shri Ram. Indeed, in the presence of hordes of monkeys and bears, the extermination of his enemies by whom Sita was actually laid violent hands upon was vowed by Shri Ram. Assuredly, doing wrong to Shri Ram, not even Indra himself can attain happiness, much less an ordinary individual like you. Know her whom you recognize by the name of Sita and who stays in your abode to be none else than kalaratri, bent upon destroying the whole of Lanka. Therefore, have done with the noose of death in the form of Sita’s personality, which has been placed by yourself about your neck. And let the means of your own safety be devised. Behold this city of Lanka, which has already been consumed by the moral power of Sita and set on fire by the wrath of Shri Ram, burning with its palaces and streets. Pray don’t lead to annihilation of your own allies…… and Lanka itself. Listen, O suzerain lord of ogres, to my truthful assertion, coming as it is from the lips of a servant of Shri Ram, an envoy and particularly a monkey.
Canto 51 - Sundara Kanda, Valmiki Ramayana
After this, enraged by Hanuman’s words, Ravana tries to kill the envoy. On being forbidden of such an act, which is against Shastras, Hanuman’s tail is lit, which he uses to set the entire Lanka on fire.
Thus, yes Hanuman was held captive by Ravana, but it was under his own wish because he wanted to know about Ravana and learn about his strength as well as warn him of his actions.
