Former Gov. George E. Pataki testified in a Manhattan civil trial on Tuesday that among the reasons he pushed to keep convicted sexual offenders confined to psychiatric hospitals after they had completed their prison sentences was an episode that occurred years earlier and touched his family directly.

He said that he, his family and some neighborhood children were hiking in a state park in the mid-1990s when they found themselves being followed by a man who Mr. Pataki later learned had been convicted of sexual crimes in the Rochester area.

“My family was fine — I had troopers,” Mr. Pataki told the jury in Federal District Court. “But I couldn’t help but think of a mother in a walk in the park with a child.”

He added, “It just made me personally aware not just of the horrors of these crimes, but the immediacy of the possibility.”

After the court proceeding ended, he told reporters that the location had been the Hudson Highlands State Park, and that he did not believe he had previously discussed the matter publicly.

In his testimony, Mr. Pataki also cited the 2005 murder of a woman in a parking garage next to the Galleria mall in White Plains, N.Y. The assailant had been a sexually violent criminal who had been released from prison, was “obviously mentally ill” and had claimed that he was not getting the mental health treatment that he deserved, Mr. Pataki said.

Mr. Pataki’s testimony came in the third week of a trial stemming from a lawsuit by six men who claim that, under a 2005 Pataki administration initiative, they had been unlawfully confined to state mental hospitals after serving prison sentences for sexually violent crimes.