Evidently this mockery happened during Peter's denial and at the end of Jesus' hearing before Caiaphas. Luke probably placed it here in his narrative as a transition to contrast Peter's attempts to avoid suffering with the sufferings of Jesus. It introduces Luke's accounts of Jesus' trials. Luke's is the longest of the synoptic accounts. It presents Jesus as a real man suffering unjustly at the hands of His accusers.
The men holding Jesus in custody were the religious leaders (v. 52; cf. Matt. 26:66-67; Mark 14:64-65). Luke presented Jesus as a prophet. He probably included this incident to show that Jesus' failure to prophesy was not due to inability but to His purpose to lay down His life as a sacrifice. Jesus' passive acceptance of all this foul treatment shows the same thing.