It was Spock who brought him out of the memories, at least partially.
In his mind, his was thirteen again, watching bodies fall and hearing the hopeless cries of those who were next, when suddenly a Vulcan appeared beside him, amazing and out of place in the strange environment. Jim Kirk realized he wasn't a boy anymore and he knew who the other man was. His relief was a living entity when he breathed out simply, "Spock!"
The Vulcan looked on at what was happening, expression impassive as he stated, "Captain, the entire bridge is being affected similarly. A message that Lt. Uhura intercepted contained some sort of weapon that is able to attack our minds. I was able to convince myself that what I was experiencing was not logical, and initiate this mind meld with you, but the memories are still present in my mind. If you will share with me what is happening then perhaps I can help you do the same."
So Jim showed him, feeling guilty to place such horror in his mind but unable not to show it to Spock, to share it with someone. They reached the end and this time he was able to continue to the events' conclusions, to safety and family and food and Earth.
Then he did the same with Spock through the mindmeld, experiencing his first officer's pain at always being different. His childhood being ridiculed for being too human and the shame at being unable to control himself adequately. Memories of harsh words and fists, disdaining expressions and small signs of prejudice. The bullying he was forced to endure, the insults aimed at his family growing up, His father's disapproval when he left for Earth, and then discovering that if he was too human on Vulcan he was still too Vulcan for Earth. The isolation and loneliness, the fear he would never be accepted anywhere in the universe. His mother, the one person who accepted him completely, lost to him forever in Vulcan's destruction.
Helping Spock work past that was easier for Jim. Moving him into the memories of Spock's caring from Nyota, the respect and friendship of the crew, the deep bond and affection and sheer need of his presence from his Captain that grew stronger daily. The emotions and self-acceptance Jim had sensed through the mind meld with the older, Ambassador Spock. All these things were sufficient to break the cycle of painful returning as he finally felt like he was within his own body and he watched numbly as Spock moved to the communications panel and cut off the transmission.
Jim began moving around the bridge, trying to access everyone's condition. Spock was holding Nyota in his arms, and it was obvious she was crying. Jim placed a handon Sulu's shoulder and the hand that reached up to clasp him was trembling. They both turned to Chekov and found him with his knees pulled to his chest and tears silently streaming down his face. "Chekov..." Kirk called quietly. When there was no response still it was Sulu who moved forward to embrace him, softly whispering "Pavel, it's alright now..."
At that moment, Yeoman Rand came running onto the bridge followed by an out of breath Dr. McCoy. "Dammit Jim! We didn't know what was going on but we sealed off the Bridge trying to contain it. What the hell happened??!"Bones began examining each of them in turn, before ordering them all the medbay for further testing. It was a sign of how traumatized they all were that no one, not even Spock protested. Medical scanning showed they were all stressed, but with no lasting harm.
The science division was working on the booby trapped message, along with communication officers. Looking at his bridge crew's shell shocked faces, Jim decided none of them needed to deal with the aftermath of this alone, including himself."Bones has cleared us all but wants us on R&R until tomorrow. I want to see all of you in the rec room at 0900 hours. That IS an order."Then he went back to his quarters and collapsed until he was awoken by the door chime.
