Jeremiah received the LORD's word after Nebuzaradan the captain of the special guard had released him from Ramah. He had been bound in chains there along with all the other detainees from Jerusalem and Judah who were being sent off to Babylon.
References for Jeremiah 40:1
The captain of the special guard located Jeremiah and said to him, "The LORD your God declared that a great disaster would overtake this place.
Now the LORD has made it happen. He has done just as he warned because all of you have sinned against the LORD and haven't obeyed him. That's why this has happened to you.
But I'm setting you free from the chains on your hands. If you would like, come with me to Babylon, and I'll take care of you. If you would rather not come with me, that's fine too. Now, the whole land lies before you; go wherever you want.
If you decide to remain here, stay with Gedaliah, Ahikam's son and Shaphan's grandson—the Babylonian appointee in charge of the cities of Judah. Stay with him and the people he rules or go wherever you want." Then the captain of the special guard gave him ample provisions and let him go.
References for Jeremiah 40:5
Jeremiah went to Gedaliah, Ahikam's son at Mizpah, and he stayed with him and the people who remained in the land.
Some of the army officers and their troops were still hiding out in the countryside when they heard that the king of Babylon had appointed Gedaliah, Ahikam's son, over the region, responsible for the men, women, and children who were the poorest in the land and who hadn't been deported to Babylon.
References for Jeremiah 40:7
So they went out to meet Gedaliah at Mizpah: Ishmael, Nethaniah's son; Johanan and Jonathan, Kareah's sons; Seraiah son of Tanhumeth; the sons of Ephai the Netophathite; Jezaniah son of the Maacathite; and their troops.
Gedaliah, Ahikam's son and Shaphan's grandson, firmly assured them all: "Don't be afraid of serving the Babylonians. Remain in the land, serve the king of Babylon, and all will go well for you.
But me? I will stay at Mizpah so I can speak on your behalf when the Babylonians arrive. But you? Settle down in the towns you have taken; harvest the grapes, the summer fruits and figs, and then store them in your containers."
In the same way, all the Judeans living in Moab, Ammon, Edom, and in other countries heard that the king of Babylon had left a few people in the land and that he had put Gedaliah, Ahikam's son and Shaphan's grandson, in charge of them.
So they left the places where they had been scattered and returned to the land, to Gedaliah at Mizpah. There they gathered large amounts of grapes and summer fruits.
Johanan, Kareah's son, and all the army officers in the countryside approached Gedaliah at Mizpah
and said to him, "Are you aware that King Baalis of Ammon has sent Ishmael, Nethaniah's son, to kill you?" But Gedaliah, Ahikam's son, wouldn't believe them.
Still Johanan, Kareah's son, met with Gedaliah secretly at Mizpah and said to him, "Let me go and kill Ishmael, Nethaniah's son; no one needs to knows about this matter. Otherwise, he'll kill you, and all the Judeans who have gathered around you will be scattered, and the few who are left will perish."
But Gedaliah son of Ahikam told Johanan, Kareah's son, "Don't do such a thing, for what you are saying about Ishmael is wrong."